Hello guys, I’ve just started to develop in Unity/C# . I 'm experienced in coding ( java and python) but not in C# and neither in Unity. To gets my hands on Unity, I have followed this great tutorial for re-developping flappy bird.
I was half way when I got stuck by a null reference:
console said:
NullReferenceException: Object reference not set to an instance of an object
TapController.OnTriggerEnter2D (UnityEngine.Collider2D col) (at Assets/scripts/TapController.cs:73)
when the bird hit the ground, the code didn’t end well.
the lines are here:
void OnTriggerEnter2D(Collider2D col){
if(col.gameObject.tag=="ScoreZone"){
//register a score amount
print("OnGameOverConfirmed");
OnPlayerScored(); //event sent to GameManager;
//play a sound
}
if (col.gameObject.tag=="DeadZone"){
rigidbody.simulated=false;
Debug.Log("DeadZone");
//register a dead event
OnPlayerDied(); //event sent to GameManager; NULL HERE !!!
//play a sound
}
}
I’m a noob in unity and C# so I spent several hours finding the problem…
It turns out it was just an extremely dumb miswriting in
GameManager.cs
void onEnable(){
Debug.Log("OnEnable GameManager");
CountdownText.OnCountdownFinished+=OnCountdownFinished;
TapController.OnPlayerDied+=OnPlayerDied;
TapController.OnPlayerScored+=OnPlayerScored;
}
correct spelling was “OnEnable()”
and same in TapController.
I was trying to install debugger in C# would that have helped me? or not at all?
How can i find such mistakes when the console points on a totally ( to me) unrelated problem?
Do you have any advices?
Thanks for your help