Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
1
Question by Grimshad · Apr 28, 2013 at 05:07 AM · colliderontriggerentercolliding

Null reference that isn't null

I keep getting this error:

 NullReferenceException: Object reference not set to an instance of an object
 sPlayer.CollisionEvent (UnityEngine.GameObject other) (at Assets/Scripts/Actors/sPlayer.js:111)
 sPlayerCollider.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Actors/sPlayerCollider.js:31)

I have 3 objects here, the player, the players collider (because the mesh won't import into unity with the correct rotation) as a child of the player. Then the enemy.

When the enemy and the players collider collide it triggers the OnTriggerEnter function in the players collider script.

 function OnTriggerEnter(other :Collider)
 {
     if(other != null)
         transform.parent.GetComponent(sPlayer).CollisionEvent(other.gameObject);
     else
         print("other is null");
 }

Note there is a null check. this function then calls the real collision function on the parent (player) script and feeds it the enemy object it just collided with.

 function CollisionEvent(other:GameObject)
 {
     if(other != null)
     {
         if(other.tag == "asteroid")
         {
             if(shieldActive)
             {
                 playerShield.GetComponent(sShield).DestroyMe();
                 other.GetComponent(sAsteroid).Damage(transform.position, true);
                 shieldActive = false;
             }
             else
             {
                 //Handle hud & game over
                 theManager.UpdateLives(-1);
                 other.GetComponent(sAsteroid).Damage(transform.position, true);
             }
         }
         else if(other.tag == "speedpu")
         {
             other.GetComponent(sPowerup).DestroyMe();
             theManager.GetComponent(sManager).TextPopUp("Rapid Fire", Color.green, other.transform.position, other.transform.rotation);
             SpeedBoost();
         }
         else if(other.tag == "shieldpu")
         {
             theManager.UpdateShieldCount(1);
             theManager.GetComponent(sManager).TextPopUp("Shield", Color.blue, other.transform.position, other.transform.rotation);
             other.GetComponent(sPowerup).DestroyMe();
         }
         else if(other.tag == "multipu")
         {
             other.GetComponent(sPowerup).DestroyMe();
             theManager.GetComponent(sManager).TextPopUp("Multi Shot", Color.red, other.transform.position, other.transform.rotation);
             MultiShot();
         }
     }
     else
         print("no game object found");
 }


Note again the null check. It never prints to the console telling me that the object was null, which tells me that there is an object referenced. If I print other, it returns an object reference. It correctly checks the tag of other and goes into the correct conditional, but it never calls any functions on other and never gives an error saying it can't.

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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Johnnemann · Apr 28, 2013 at 06:49 AM

The error says it's on line 111 of your file. What is on that line? You're doing a lot of GetComponent().. I suspect that one of those objects doesn't actually have the component that you're looking for.

For example, in this line:

 other.GetComponent(sPowerup).DestroyMe();

If other doesn't have a component sPowerup, you will get a null reference exception.

Rather than using tags, you could do:

 AsteroidComponent:sAsteroid = other.GetComponent(sAsteroid);
 
 if(AsteroidComponent != null)
 {
    //do asteroid code here
 }
 
 PowerUpComponent:sPowerup = other.GetComponent(sPowerup);
 
 if(PowerUpComponent)
 {
    // do power up code
 }
 //etc
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 Grimshad · Apr 28, 2013 at 04:18 PM 0
Share

Turns out I lost a object reference from the inspector, so I just referenced the object via script so i don't have to worry about it anymore. Thanks.

avatar image
2

Answer by Eric5h5 · Apr 28, 2013 at 05:11 AM

If you get a null reference exception error, that means 100% of the time that you're trying to reference something that doesn't exist. There is never a case where this isn't true. Your GetComponent statements can be resulting in a null reference if the component isn't attached, for example, and you aren't checking those for null.

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnTriggerEnter (JS) 1 Answer

Check trigger/collider once? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can't figure out how to use multiple triggers in single scene 1 Answer

Make a collider un-passable unless condition is met? 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