Search Unity

Error NullReferenceException: Object reference not set to an instance of an object

Discussion in 'Scripting' started by BrokenhearT, Jul 4, 2015.

  1. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    Hello everyone, I'm having a weird issue been looking around for a solution and nothing so far, so I'd like to know why am I getting this error?



    Here's the script attached, how to fix it so I don't get this error?

    Code (CSharp):
    1.     public Transform monsterTransform;
    2.     private MonsterEnemy monster;
    3.  
    4.  
    5.     void Start ()
    6.     {
    7.         monster = FindObjectOfType<MonsterEnemy>();
    8.     }
    9.  
    10.  
    11.     void Update ()
    12.     {
    13.         if (monster.enabled == true)
    14.         {
    15.         Destroy(GameObject.FindGameObjectWithTag("Enemy"));
    16.         }
    17.         if (monster.enabled == false)
    18.            
    19.             return;
    20.     }
    21.  
    22.     void FixedUpdate ()
    23.     {
    24.         monsterTransform = GameObject.FindGameObjectWithTag("Monster").transform;
    25.         if (monster.enabled == true)
    26.             Destroy(GameObject.FindGameObjectWithTag("Enemy"));
    27.         if (monster.enabled == false)
    28.             return;
    29.     }
    30. }
    Thanks in advance.
     
  2. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Because some object reference is not set to an instance of an object. Since you're not posting the entire error message, I can't know which, but it's either that monsterTransform is not set in the inspector, FindObjectOfType<MonsterEnemy>() does not find a MonsterEnemy in the scene, or possibly GameObject.FindGameObjectWithTag("Enemy") doesn't find an object with the Enemy tag.
     
  3. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    Here's the error message all of it.

    NullReferenceException: Object reference not set to an instance of an object
    DestroyOthersIfMonsterIsOn.Update () (at Assets/MyGameScripts/DestroyOthersIfMonsterIsOn.cs:18)
     
  4. SkillBased

    SkillBased

    Joined:
    Aug 11, 2014
    Posts:
    141
    So on line 18 of DestroyOthersIfMonsterIsOn.cs you should expect to find an object that is not necessarily holding a reference to an object instance.

    Maybe you expect that object at that line of code to later hold a reference? Or perhaps its de-referenced at runtime because something else in code destroyed it already? Both common situations...
     
  5. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Ok, so line 18 in your code isn't line 18 in what you pasted, so I'm still a bit unsure, but I'm going to guess it's the line
    Code (csharp):
    1. if(monster.enabled==true)
    If this is the case, the following method returns null because it couldn't find a MonsterEnemy in the scene:
    Code (csharp):
    1. monster = FindObjectOfType<MonsterEnemy>();
    Remember that when you call this in Start, they have to be in the scene when the level starts. To confirm if this is the case you can try to do something like this
    Code (csharp):
    1. void Start() {
    2.     monster = FindObjectOfType<MonsterEnemy>();
    3.     if (monster == null) { Debug.Log("Monster not found"); }
    4. }
    This will print a message in the log if a MonsterEnemy isn't found at start.
     
  6. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    Still same issue, didn't fix it, I did all you mentioned, how can I do it is there a way around that when this monster appears every other enemy tagged object be destroyed? he has a timer he appears every 6 mins.
     
  7. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    Solved, I had the script also attached to something else another game object and it was causing that issue, thanks a lot for the suggestions!