Search Unity

Question Need help with a tutorial

Discussion in 'Audio & Video' started by AleeChris, Apr 12, 2021.

  1. AleeChris

    AleeChris

    Joined:
    Apr 12, 2021
    Posts:
    1
    Hi everyone. Im'm a beginner using Unity and learning codes, so I gave it a try and started making a simple game following this tutorial https://learn.unity.com/tutorial/live-session-making-a-flappy-bird-style-game?signup=true
    The thing is, I'm having troubles with the scripts. The guy in the tutorial uses different scripts for videos and website and I'm following the videos.

    "NullReferenceException: Object reference not set to an instance of an object
    Plane.OnCollisionEnter2D (UnityEngine.Collision2D other) (at Assets/scripts/Plane.cs:35)"

    This is the error message. I have fixed previous errors and maybe I made a new one. These are "Plane" "GameController" scripts which have been giving problems :C Thanks to anyone who responds

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Plane : MonoBehaviour
    6. {
    7.  
    8. public float upForce = 300f;
    9.  
    10.     private bool isDead = false;
    11.     private Rigidbody2D rb2d;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         rb2d = GetComponent<Rigidbody2D> ();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         if (isDead == false)
    23.         {
    24.             if (Input.GetMouseButtonDown (0))
    25.             {
    26.                 rb2d.velocity = Vector2.zero;
    27.                 rb2d.AddForce(new Vector2(0, upForce));
    28.             }
    29.         }
    30.     }
    31.  
    32.     void OnCollisionEnter2D(Collision2D other)
    33.     {
    34.         isDead = true;
    35.         GameControl.instance.PlaneDied ();
    36.    
    37.     }
    38. }
    39.  


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameController : MonoBehaviour
    6. {
    7.    
    8.     public static GameController instance;
    9.     public GameObject GameOverText;
    10.     public bool gameOver = false;
    11.  
    12.     // Start is called before the first frame update
    13.     void Awake ()
    14.     {
    15.         if (instance == null)
    16.         {
    17.             instance = this;
    18.         }
    19.         else if (instance != this)
    20.         {
    21.             Destroy (gameObject);
    22.         }
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.        
    29.     }
    30.  
    31.     public void PlaneDied ()
    32.     {
    33.         GameOverText.SetActive (true);
    34.         gameOver = true;
    35.     }
    36. }
    37.