# nullException issue [duplicate]

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.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?

• And where/how do you expect plyC to be initialized? – Vaillancourt Jun 5 '17 at 0:03
• I initialize it in 'mainmanager' as private playerController plyC – Jihen007 Jun 5 '17 at 0:06
• 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! – Vaillancourt Jun 5 '17 at 0:08
• post Edited ... – Jihen007 Jun 5 '17 at 0:34
• 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? – DMGregory Jun 5 '17 at 2:01

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.