Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

"Object reference not set to instance of an object"

Discussion in '2D' started by TurduckenMan, Apr 20, 2014.

  1. TurduckenMan

    TurduckenMan

    Joined:
    Apr 20, 2014
    Posts:
    29
    I'm attempting to create little waypoints that when touched by the player increase the score by one. However, everytime the player touches the collider, my score will go up by one, but the collider's then removed so that it's no longer cycling through my game when the looper reaches it.

    Here's my code for the collider/hitting obstacles:

    Code (csharp):
    1. void OnTriggerEnter2D(Collider2D collider)  {
    2.                 if (godMode)
    3.                         return;
    4.                 if (collider.tag == "ScoreBox") {  //The error is here
    5.                         Score.AddPoint ();
    6.                         gameObject.SetActive(false);
    7.                 } else {
    8.                         animator.SetTrigger ("Dead");
    9.                         dead = true;
    10.                 }
    11.         }
    The error I'm receiving is saying that The object reference is not set to an instance of an object, which is beyond me as to how I' go about fixing.
    Thanks for any help :)
     
  2. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    I don't get it. You're setting the Game Object to inactive, and complaining that it's gone? If you don't want it to be gone, why are you inactivating it?
     
  3. TurduckenMan

    TurduckenMan

    Joined:
    Apr 20, 2014
    Posts:
    29
    Sorry I didn't specify more clearly what all I had going on.
    The script is inside of the player and that's there to make the player inactive so that he doesn't trigger the score box multiple times.
     
  4. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    So... How does it become active again? Or is that your question? You want to score the same zone repeatedly, but only once per visit?

    Maybe try using an OnTriggerExit2D event function to re-enable your scoring. Of course, you can't do this if it's disabled, so you'll have to set a more specific flag instead. Something like:

    Code (csharp):
    1. private bool ScoreActive = true;
    2.  
    3. void OnTriggerEnter2D(Collider2D collider)  {
    4.     if (godMode)
    5.         return;
    6.     if (collider.tag == "ScoreBox") {
    7.         if (ScoreActive) {
    8.             Score.AddPoint ();
    9.             ScoreActive = false;
    10.         }
    11.     } else {
    12.         animator.SetTrigger ("Dead");
    13.         dead = true;
    14.     }
    15. }
    16.  
    17. void OnTriggerExit2D(other: Collider2D) {
    18.     if (collider.tag == "ScoreBox") ScoreActive = true;
    19. }
     
  5. TurduckenMan

    TurduckenMan

    Joined:
    Apr 20, 2014
    Posts:
    29
    Thanks man, the only issue I'm having with that is it's flagging the "other" in (other: Collider2D) as an error and is asking for an identifier instead.
    I feel like this'll probably solve the problem I just can't try it until this error's gone haha.
     
  6. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Code (csharp):
    1.  
    2. other : Collider2D
    3.  
    Is JavaScript syntax. It should be:

    Code (csharp):
    1.  
    2. Collider2D other
    3.  
    EDIT: And it looks like it should actually be

    Code (csharp):
    1.  
    2. Collider2D collider
    3.  
    to follow along with the code's naming
     
  7. TurduckenMan

    TurduckenMan

    Joined:
    Apr 20, 2014
    Posts:
    29
    Thanks that fixed the error! Unfortunately now though the same thing's still happening. :/ Am I maybe not naming the box the right thing or something? That's all I can think for why it's telling me the object reference is not set to an instance of an object. Sorry if I'm not grasping this fantastically, I'm still pretty new to the whole thing.
     
  8. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    Did you tag the score box object in the editor?
     
  9. TurduckenMan

    TurduckenMan

    Joined:
    Apr 20, 2014
    Posts:
    29
  10. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    Describe exactly what's happening for me again could you
     
  11. TurduckenMan

    TurduckenMan

    Joined:
    Apr 20, 2014
    Posts:
    29
    Basically, my game is made up of these things:

    Obstacles
    Background
    Player
    ScoreBox
    Looper

    Basically, the looper is connected to the camera and is following just out of view. The code on it makes the background and obstacles loop back to the front of their "line" so that the background repeats, and obstacles appear endlessly.
    The problem is that when the looper touches the 2D box collider I've created to be ScoreBox, rather than send it back to it's new position at the end of the line, it simply deletes it and says "NullReferenceException: Object reference not set to an instance of an object"

    Thanks again, tried to be as specific as possible

    EDIT: Also I'm using the new code Pyrian gave me with the correction DanielQuick gave me.

    Here it is again:
    Code (csharp):
    1. private bool ScoreActive = true;
    2.  
    3. void OnTriggerEnter2D(Collider2D collider)  {
    4.        
    5.         if (godMode)
    6.            
    7.             return;
    8.        
    9.         if (collider.tag == "ScoreBox") {
    10.            
    11.             if (ScoreActive) {
    12.                
    13.                 Score.AddPoint ();
    14.                
    15.                 ScoreActive = false;
    16.                
    17.             }
    18.            
    19.         } else {
    20.            
    21.             animator.SetTrigger ("Dead");
    22.            
    23.             dead = true;
    24.            
    25.         }
    26.        
    27.     }
    28.    
    29.    
    30.    
    31.     void OnTriggerExit2D(Collider2D collider) {
    32.        
    33.         if (collider.tag == "ScoreBox") ScoreActive = true;
    34.        
    35.     }
     
    Last edited: Apr 21, 2014
  12. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    I'm still not totally understanding the setup but do you use any Destroy() methods elsewhere? Didn't forget to remove old script that disabled the scripted object did you?
     
  13. TurduckenMan

    TurduckenMan

    Joined:
    Apr 20, 2014
    Posts:
    29
    Yeah I disabled the old script, and no there aren't any Destroy's anywhere. The score box is a child to the obstacles object if that makes a difference, they're just seperate in that they're different colliders.
     
  14. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    Hmm well if your getting null reference exception something's not being found when your code asks for it. If it's the line for OnTriggerExit maybe check if if(collider != null) before doing if tag part

    Something like

    Code (csharp):
    1.  
    2.   void OnTriggerExit2D(Collider2D collider) {
    3.  
    4.        
    5.  
    6.         if (collider != null  collider.tag == "ScoreBox") ScoreActive = true;
    7.     }
    8.  
     
    Last edited: Apr 21, 2014
  15. TurduckenMan

    TurduckenMan

    Joined:
    Apr 20, 2014
    Posts:
    29
    How and where exactly would I add that?
    Thanks again for all the help I'm still learning C# haha.
     
  16. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    See edit above
     
  17. TurduckenMan

    TurduckenMan

    Joined:
    Apr 20, 2014
    Posts:
    29
    Hmm, it's still giving me the same error. I'm thinking maybe I should just make the script destroy them and create a spawner of some kind instead?
     
  18. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    It sounds like a different implementation might work better. If I knew exactly what was going on it might help lol, is it like a coin launcher at spinning zombie platform game? Haha

    Edit doh no it's not, where did I get that crazy idea from? Lol
     
  19. TurduckenMan

    TurduckenMan

    Joined:
    Apr 20, 2014
    Posts:
    29
    No it's actually a flappy bird rip off I'm making for my friend and basically the scoreboxes in between the pipes are what's giving me the issue :p
    Thanks anyway for trying man, haha.
     
  20. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    Well try maybe using Debug.Log(scoreActive); in Update to see when it changes, and to what
     
  21. TurduckenMan

    TurduckenMan

    Joined:
    Apr 20, 2014
    Posts:
    29
    That's not working out for me. Do you think the fact that I have this code:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Score : MonoBehaviour {
    5.  
    6.     static int score = 0;
    7.     static int highScore = 0;
    8.  
    9.     static public void AddPoint() {
    10.                 score++;
    11.  
    12.                 if(score > highScore) {
    13.                         highScore = score;
    14.                 }
    15.         }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.         guiText.text = "Score: " + score + "\nHigh Score: " + highScore;
    20.     }
    21. }
    ...as it's own thing? Should this be with the other stuff?
     
  22. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,987
    I would start by putting a Debug.Log(collider) right before if statement.

    My guess is that is being called multiple times or hitting something aren't expecting. When you run check the console and see if you are getting more than one "hit". It will help give you some insight into what is happening and in what order.