Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by januskirin · Aug 05, 2011 at 12:10 AM · collisionnullreferenceexceptionontriggerenterwalker

Sending C# commands to an OnTriggerEnter's collider object

I'm doing the Walker Boys Studio lab 2, part 33. But I'm writing all the scripts in C# to get that sweet auto-complete goodness.

The issue I'm running into occurs when the asteroid hits the player's shield. Pretty sure they're both set up right (a sphere with a sphere collider, IsTrigger=true, and a rigidbody).

Here's the code that runs for the asteroid:

 void OnTriggerEnter(Collider other)
 {
     if (other.tag == "shield")
     {
         HitShield(other.gameObject);
         ExplodeAsteroid();
         ResetPosition();
     }
 }
 
 void HitShield(GameObject other)
 {
     scriptShield localShield = (scriptShield)other.GetComponent(typeof(scriptShield));
     localShield.HitShield();
 }

The error I'm getting is NullReferenceException: Object reference not set to an instance of an object, which occurs when I try to call localShield.HitShield();. Does it matter that the shield object is a prefab which is instantiated by the player prefab?

How can the object be null if I can read its tags? There's something fundamental I'm missing, right?

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

1 Reply

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

Answer by Bunny83 · Aug 05, 2011 at 12:27 AM

The GameObject is not null, that's kinda impossible ;) I guess the Script component you're trying to access is not there. Keep in mind that "other" referes to a collider that is attached to an GameObject tagged "shield". Your script have to be attacht to the same GameObject.

Btw. there are generic versions of GetComponent, GetComponents and GetComponentInChildren that doesn't require you to cast the returnd type "manually":

 scriptShield localShield = other.GetComponent<scriptShield>();

If your shild-script is attached to a child of the collider you can use

 scriptShield localShield = other.GetComponentInChildren<scriptShield>();

also in such cases when you're not sure if the component is really there, you can do a check yourself:

 scriptShield localShield = other.GetComponentInChildren<scriptShield>();
 if (localShield != null)
 {
     localShield.HitShield();
 }
 else
 {
     Debug.LogWarning("Hit shield but no scriptShield found",this);
 }
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 januskirin · Aug 05, 2011 at 01:09 AM 0
Share

Damn, my first question and it's a "you forgot to link the script to the prefab" error. That was my mistake. Thanks, Bunny83. I'll pay more attention from now on. :-)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException on OnTriggerEnter 0 Answers

Why is this not working? 0 Answers

How to only delete one of two collided objects? 1 Answer

OnTriggerEnter not firing if rigidbody is on its side 0 Answers

OnTriggerEnter detection too late 0 Answers


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