• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Oct 04, 2015 at 07:21 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Tabu · Sep 19, 2010 at 03:08 PM · nullreferenceexception

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by duck · Sep 19, 2010 at 03:59 PM

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tabu · Sep 19, 2010 at 04:33 PM 0
Share

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 damage$$anonymous$$anagement 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 :)

avatar image
0

Answer by Tabu · Sep 19, 2010 at 05:45 PM

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);
     }
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Gameobject not null at start but in function it is. 2 Answers

NullReferenceException by localRotation And Quaternion.Euler!?? 1 Answer

Null Reference Exception when using variable in another script 2 Answers

Scene Null Reference in build 1 Answer

Why does ScriptableObject.CreateInstance, where T is definite at runtime, return null? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges