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 /
This question was closed May 11, 2013 at 07:54 AM by Fattie for the following reason:

Duplicate Question

avatar image
0
Question by Pauls · May 11, 2013 at 06:36 AM · c#triggernullreferenceexception

NullReference with trigger C# ?

Hi,

I don't understand why I get this error in my code :

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

CornerIA1.OnTriggerEnter After the collision, triggerOn = true should be sent to the IAParent script, the scripts are well connected to their gameObject, I also tried with GetComponent<> or transform.GetComponent... but still the same thing. Here is the code : //IAParent scr; void Start () { }
void Update () { }
void OnTriggerEnter ( Collider other ) { IAParent scr = other.GetComponent("IAParent") as IAParent; scr.triggerOn = true;//null reference } Would you know why? Thanks
Comment
Add comment · Show 4
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 Ninita · May 11, 2013 at 09:32 AM 0
Share

"other" isn't a GameObject, and it migth be to do what you want. try other.gameObject.GetComponent("IAParent") as IAParent;. about the GetComponent method I prefer doing GetComponent() , works better

avatar image Pauls · May 11, 2013 at 11:37 AM 0
Share

@Ninita thanks, unfortunately no. It still shows the error on scr.triggerOn... if I remove it, I don't get the error anymore.

avatar image Ninita · May 11, 2013 at 11:47 AM 0
Share

do

 IAParent scr = other.gameObject.GetComponent<IAParent>();
 
 if(!scr)
 {
   Debug.Log("scr don't exist");
   scr = other.gameObject.AddComponent("IAParent");
 }
 
 scr.triggerOn=true;

that error says that gameObject with that collider don't have any component IAParent, and with this code you guarantee it have

avatar image Pauls · May 11, 2013 at 02:38 PM 0
Share

@Ninita thanks Ninita! You were right, it says "does not exist", but there is the script attached to the gameobject though. Anyway, I don't have the error anymore with this AddComponent, thanks a lot! @Fattie this is the correct answer, not the one you selected.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Karsten · May 11, 2013 at 07:19 AM

not tested

what happens if you use something like this?

 //IAParent scr;
  
 void Start () { }
  
 void Update () { }
  
 void OnTriggerEnter ( Collider other ) {
 IAParent scr = other.gameObject.GetComponent("IAParent") as IAParent;
 scr.triggerOn = true;//null reference
 }
Comment
Add comment · Show 3 · 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 Pauls · May 11, 2013 at 11:25 AM 0
Share

Thanks @$$anonymous$$arsten, nop, I tried this first. I still get the same error on scr.triggerOn = true ... @Fattie sorry maybe there is a mistake with the closure of this topic, could you re-open it? it works fine for "scr = ..." but it does not work with "src.triggerOn..." . triggerOn is "public" in my other class. Thanks

avatar image Karsten · May 11, 2013 at 12:02 PM 0
Share

hm very strange, can you proof that scr has a value after IAParent scr = other.gameObject.GetComponent("IAParent") as IAParent; ? also i think you can left out "as IAParent" because its implicit said in the declaration IAParent scr =

maybe triggerOn is a local variable in IAParent?

avatar image Pauls · May 11, 2013 at 02:41 PM 0
Share

Thanks @$$anonymous$$arsten for your help, I could make it work with Ninita's code, thanks anyway. Really appreciated!

Follow this Question

Answers Answers and Comments

15 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 avatar image

Related Questions

Distribute terrain in zones 3 Answers

Can't find source of trigger Null Reference Exception 1 Answer

Multiple Cars not working 1 Answer

NullReferenceException yet Debug.Log shows nothing is null? 1 Answer

Working Reference Still Throwing Null Reference Error 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