0
\$\begingroup\$

I have a problem when I was trying to change a state within my character. I wrote the code below

 public class playerController : MonoBehaviour {
 private CharacterController characterC;
 private Animator anim;
 // Use this for initialization
 void Start () {
     characterC = GetComponent<CharacterController> ();
     anim = GetComponent<Animator> ();

 }

 // Update is called once per frame
 void Update () {

 }
 // put a line attack!
 public void Attack(){

     anim.SetBool ("attack", true); 
     anim.Play ("attack");
 }
 // when we got a box
 public void Doubleattack(){
     anim.SetBool ("hitBox", true); 
     anim.Play ("doublechop");
 }
 //  dying animation gameover loser
 public void Die(){
     anim.SetBool ("isDead", true); 
     anim.Play ("die");
 }

}

and i use one of my methods in my "mainmanager"

public class MainManeger : MonoBehaviour
{

    private bool isOnlineGame=false;

    private playerController plyC;`

public void start2PlayersGame()
         {
             isOnlineGame = false;
             StartLevel();
             PlayerManeger.set1VS1players();
             AnimationManeger.showGridWindow();
             plyC.Attack ();
             SoundManeger.PlayBackgroudOnPlay();
         }}`

I got this :NullReferenceException: Object reference not set to an instance of an object at ''plyC.Attack ()'' why?

\$\endgroup\$
5
  • \$\begingroup\$ And where/how do you expect plyC to be initialized? \$\endgroup\$ Commented Jun 5, 2017 at 0:03
  • \$\begingroup\$ I initialize it in 'mainmanager' as private playerController plyC \$\endgroup\$ Commented Jun 5, 2017 at 0:06
  • \$\begingroup\$ I would just answer 'No' but that would be rude :P If it were properly initialized, you would not get that exception. Please edit the post and add the code where you think plyC is initialized! \$\endgroup\$ Commented Jun 5, 2017 at 0:08
  • \$\begingroup\$ post Edited ... \$\endgroup\$ Commented Jun 5, 2017 at 0:34
  • 1
    \$\begingroup\$ Which line do you think is initializing plyC, @Jihen007? There's not a single plyC = ... anywhere in what you've shown - and since it's private and not marked [SerializeField] it wouldn't show up to be populated in the inspector either. So it looks like it would still have the default value of null that reference type variables get when they're declared. How do you intend to tell this code which playerController should perform the Attack() action? \$\endgroup\$ Commented Jun 5, 2017 at 2:01

1 Answer 1

2
\$\begingroup\$

plyC is declared, but not initialized.

I don't know about your exact needs, but maybe you want to add = new playerController() after private playerController plyC. Or maybe Unity's way is to do it in Awake or Start.

Or maybe it exists as a component somewhere else and you have to get it.

In any case, it is not sufficient to only write the line with the type and the variable name, this only creates a null reference, you'll have to assign an instance to it, and that's basically what the error is telling you :)

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.