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?
plyC
to be initialized? \$\endgroup\$plyC
is initialized! \$\endgroup\$plyC
, @Jihen007? There's not a singleplyC = ...
anywhere in what you've shown - and since it'sprivate
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 ofnull
that reference type variables get when they're declared. How do you intend to tell this code whichplayerController
should perform theAttack()
action? \$\endgroup\$