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.