Search Unity

object reference not set to an instance of object

Discussion in 'Scripting' started by TalhaJawed, Nov 20, 2019.

  1. TalhaJawed

    TalhaJawed

    Joined:
    Sep 18, 2019
    Posts:
    10
    Hello,
    I am trying to write a script for breakout game. I have a hud script which is attached to UI canvas. This script is used to update score and balls left to play.
    Code (CSharp):
    1.  
    2. public class HUD : MonoBehaviour
    3. {
    4.    
    5.     static Text scoretext;
    6.     static float actualscore = 0;
    7.     static Text ballsleft;
    8.     static float actualballsleft = ConfigurationUtils.NumberOfBallsPerGame;
    9.     public static  void blockhit()
    10.     {
    11.         actualscore += 1;
    12.         scoretext.text = "Score: " + actualscore.ToString();
    13.  
    14.     }
    15.     public static void ballsleftmethod()
    16.     {
    17.         actualballsleft -= 1;
    18.         ballsleft.text = "Balls Left: " + actualballsleft.ToString();
    19.     }
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         Text scoreetext = GameObject.FindGameObjectWithTag("score").GetComponent<Text>();
    25.         scoreetext.text = "Score: " + actualscore.ToString();
    26.         Text balleslefttext = GameObject.FindGameObjectWithTag("ballsleft").GetComponent<Text>();
    27.         balleslefttext.text = "Balls Left: " + actualballsleft.ToString();
    28.     }
    29.  
    I have another script named block which is attached to each block in the game that's going to hit.
    Code (CSharp):
    1.  
    2.  
    3. public class block : MonoBehaviour
    4. {void OnCollisionEnter2D(Collision2D collision)
    5.        
    6.     {
    7.         GameObject collider = collision.gameObject;
    8.         if (collider.CompareTag("Ball"))
    9.         {
    10.            HUD.blockhit();
    11.             Destroy(gameObject);
    12.            
    13.         }
    14.     }
    15.  
    I am trying to call blockhit() method in the from the HUD script but getting error
    "NullReferenceException: Object reference not set to an instance of an object
    block.OnCollisionEnter2D (UnityEngine.Collision2D collision)" . Any help would be greatly appreciated!!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    When you click the error, what line does it go to?
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Nevermind, I see the issue.

    in HUD, you have a static variable called "scoretext", which blockhit() depends on. When it tries to access this variable, scoretext is null.

    Why is scoretext null? Because in Start, you've found the object you want but then assigned it to a local variable named "scoreetext". "scoretext" has never been assigned.
     
    TalhaJawed likes this.
  4. TalhaJawed

    TalhaJawed

    Joined:
    Sep 18, 2019
    Posts:
    10
    it goes to line 12 on HUD script
    Code (CSharp):
    1. scoretext.text = "Score: " + actualscore.ToString();
    2.  
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Yup, as I thought. See second reply ^
     
  6. TalhaJawed

    TalhaJawed

    Joined:
    Sep 18, 2019
    Posts:
    10
    Thanks alot that worked!!