x


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

I cant by the love of all things good, understand why I get the error: NullReferenceException: Object reference not set to an instance of an object MonsterSpawner.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/MonsterSpawner.cs:25)

this is the part of the script that seems to be wrong? The particular line it is coplaining about is: if (!objectHit.getIsMonster())

void OnTriggerEnter(Collider other) { DamageManagement objectHit = (DamageManagement)other.GetComponent("DamageManagement"); if (!objectHit.getIsMonster()) { for (int i = 0; i < numberOfJotuns; i++) Instantiate(monster1, new Vector3(spawnPoint.x + 15 randomBool(), spawnPoint.y - 2, spawnPoint.z + (Random.value 10 * randomBool())), Quaternion.identity);

      Debug.Log("It Works " + randomBool());
      Destroy(this.gameObject);
  }

}

The wierd thing is that everything is working as it is supposed to do? And the if sentence works as intended as well? Its just annoying getting the error, and I would like to get rid of it.

more ▼

asked Sep 19, 2010 at 03:08 PM

Tabu gravatar image

Tabu
221 63 54 73

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

It must be the case that the object which was collided with did not have a "DamageManagement" component at all (and therefore 'objectHit' would be null).

Since you're not checking whether it's null, you'll get this error.

To correct it, try this modification, which includes an explicit check for a null reference:

void OnTriggerEnter(Collider other) { DamageManagement objectHit = (DamageManagement)other.GetComponent("DamageManagement"); if (objectHit == null) { Debug.Log("collided with "+other.name+" which has no DamageManagement!"); } else { if (!objectHit.getIsMonster()) { for (int i = 0; i < numberOfJotuns; i++) Instantiate(monster1, new Vector3(spawnPoint.x + 15 randomBool(), spawnPoint.y - 2, spawnPoint.z + (Random.value 10 * randomBool())), Quaternion.identity);

          Debug.Log("It Works " + randomBool());
          Destroy(this.gameObject);
      }
  }

}

hope this solves the problem!

more ▼

answered Sep 19, 2010 at 03:59 PM

duck gravatar image

duck ♦♦
47.8k 131 233 472

Yes, this was indeed the problem :) Thanks The reason why this happend, was becouse each character had a weapon that collided first, and they did not have a damageManagement attached. So everything worked, just not when the weapons collided(Which they always did) I had been looking for "other.name" thanks for that also :)

Sep 19, 2010 at 04:33 PM Tabu
(comments are locked)
10|3000 characters needed characters left

To those interrested, this is what the working code now looks like. Again, thanks Duck :) You made my sunday evening so much greater! :D

void OnTriggerEnter(Collider other) { DamageManagement objectHit = (DamageManagement)other.GetComponent("DamageManagement"); if (objectHit != null) { //Debug.Log("collided with " + other.name + " which has no DamageManagement!");

     if (!objectHit.getIsMonster())
     {
         for (int i = 0; i &lt; numberOfJotuns; i++)
             Instantiate(monster1, new Vector3(spawnPoint.x + 15 * randomBool(), spawnPoint.y - 2, spawnPoint.z + (Random.value * 10 * randomBool())), Quaternion.identity);

         Destroy(this.gameObject);
     }
 }

}

more ▼

answered Sep 19, 2010 at 05:45 PM

Tabu gravatar image

Tabu
221 63 54 73

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x774

asked: Sep 19, 2010 at 03:08 PM

Seen: 60227 times

Last Updated: Sep 19, 2010 at 03:08 PM