1

I'm making Flappy Bird with the help of Unity's own tutorial of it, and managed to get all the way to the video, where he shows how to add the game controller. All is good, but I can't seem to figure out how to get rid of this error:

NullReferenceException: Object reference not set to an instance of an object Bird.OnCollisionEnter2D () (at Assets/Scripts/Bird.cs:37)

This is the code for GameController:

using UnityEngine;
using System.Collections;

public class GameControl : MonoBehaviour 
{

    public static GameControl instance;
    public GameObject gameOverText;
    public bool gameOver = false;

    // Use this for initialization
    void Awake () 
    {
        if (instance == null) {
            instance = this;
        }  
        else if (instance != this)
        {
            Destroy (gameObject);
        }
    }

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

    }

    public void BirdDied()
    {
        gameOverText.SetActive (true);
        gameOver = true;
    }
}

This is Bird.cs:

using UnityEngine;
using System.Collections;

 public class Bird : MonoBehaviour {

public float upForce = 200f;

private bool isDead = false;
private Rigidbody2D rb2d;
private Animator anim;


void Start ()
{
    rb2d = GetComponent<Rigidbody2D> ();
    anim = GetComponent<Animator> ();
}


void Update ()
{
    if (isDead == false)
    {
        if (Input.GetMouseButtonDown (0))
        {
            rb2d.velocity = Vector2.zero;
            rb2d.AddForce (new Vector2 (0, upForce));
            anim.SetTrigger ("Flap");
        }
    }
}

void OnCollisionEnter2D ()
{
    isDead = true;
    anim.SetTrigger ("Die");
    GameControl.instance.BirdDied ();
}
}

I managed to get it working, I'm not sure what I exactly did, but I changed nothing in the code.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • This suggests that `GameControl.instance` is null. Please look through your code and figure out why that might be the case, since the relevant code is not here. – Serlite Aug 08 '17 at 15:08
  • Edited and added the relevant code. –  Aug 08 '17 at 15:13
  • Welcome to Stack Overflow! Take a moment to read through the [editing help](//stackoverflow.com/editing-help) in the help center. Formatting on Stack Overflow is different than other sites. The better your post looks, the easier it is for others to read and understand it. – gunr2171 Aug 08 '17 at 15:15
  • Because `Awake` is not called before you access `GameControl.instance` – Renatas M. Aug 08 '17 at 15:16
  • @Reniuz Could you explain how? I'm very new to this, so I don't really understand what I should do. –  Aug 08 '17 at 15:21
  • 1
    In the unity inspector window for the gameobject with the gamecontroller attached you should see an entry for "gameOverText". At the moment it is probably empty. You need to click on it and set it to the gameOverText control in the scene. – Weyland Yutani Aug 08 '17 at 15:28
  • Try using `Start` instead of `Awake`. Also look at this documentation on using Unity methods http://answers.unity3d.com/questions/10189/what-is-the-general-use-of-awake-start-update-fixe.html – Matt L. Aug 08 '17 at 15:29
  • On the otherhand the exception is saying the error occurs in a different script, Bird.cs line 37. IT would be useful to know when the error occurs (on startup?, when you tap the screen?) – Weyland Yutani Aug 08 '17 at 15:33
  • @WeylandYutani The error occurs when the bird collides with the ground. I have the exact same code as in the tutorial, but for some reason I have this error even after checking multiple times that I have the same code as the tutorial has. –  Aug 08 '17 at 15:37
  • Are you setting values for your public variables in the Inspector? – Matt L. Aug 08 '17 at 15:42
  • can you post the code for bird.cs too? – Weyland Yutani Aug 08 '17 at 15:43
  • i don't think this is a problem with the code, like Matt L says it's probably something isn't set in the inspector window in the editor. – Weyland Yutani Aug 08 '17 at 15:44
  • Another thing i noticed that might be relevant (probably not), your class is called GameControl not GameController. – Weyland Yutani Aug 08 '17 at 15:45
  • @WeylandYutani I noticed that as well, changed them, but still the same end result. –  Aug 08 '17 at 16:00
  • @MattL. There is pretty much nothing I can change in the Inspector for GameControl, because it says that "The associated script cannot be loaded. Please fix any compile errors and assign a valid script." –  Aug 08 '17 at 16:01
  • maybe your script file name is GameControl.cs. Make sure the script name and the class name are GameController.cs and GameController – Weyland Yutani Aug 09 '17 at 10:22
  • @WeylandYutani I already got it working, but thank you so much! –  Aug 09 '17 at 11:37
  • ok cool nice one! – Weyland Yutani Aug 09 '17 at 12:45

0 Answers0