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
0
Question by madmike6537 · Feb 07, 2013 at 12:37 AM · getcomponentontriggerenter

Trouble with OnTriggerEnter and GetComponent

UPDATE

Well I am still lost. I added a few more debug statements:

     void OnTriggerEnter(Collider col)
     {
         playerIsNearDeadMob = true;
         myGuiScript = col.gameObject.GetComponent<myGUI>();
         Debug.Log("You have entered dead corpse");
         if(myGuiScript == null)
         {Debug.Log ("Gui script is null...");}
         else
         {Debug.Log("guiscript is not null");}
     }

Now when I enter the trigger, here is the debug that prints:

You have entered dead corpse GUI Script is null... Guiscript is not null you have exited corpse

So even though I never leave the trigger it seems to think I have left the trigger. Doesnt make any sense. I tried making the trigger bigger to no avail :/

Hello,

I have a script which is on a (dead) mob and uses OnCollisionEnter to detect that the player is near via a sphere collider, and once the player has entered it, it uses GetCompenent to access the a script on the player called myGUI. It then it turn runs a script which opens a loot window for the player to loot items.

The problem I am having is that the sphere collider detects the player, but I am getting a null reference from the GetCompenent. When I try to access the variable myGuiScript, its null.

Would you mind taking a look and seeing if you can find why myGuiScript would be null?

Here are the pertinent parts of the script:

 private myGUI myGuiScript;
 public bool playerIsNearDeadMob = false;
 
 public void OnMouseUp()
     {
         if(myGuiScript == null)
             Debug.Log ("Player is not close enough to the loot");
         
         else if(playerIsNearDeadMob)
         {
             switch(state)
             {
             case State.open:
                 state = MobLoot.State.inBetween;
                 StartCoroutine("Close");
                 break;
             case State.closed:
                 state = MobLoot.State.inBetween;
                 StartCoroutine("Open");
                 break;
             }
         }
         
     }
 
     void OnTriggerEnter(Collider col)
     {
         playerIsNearDeadMob = true;
         myGuiScript = col.transform.GetComponent<myGUI>();
         Debug.Log("You have entered dead corpse");
     }
     
     void OnTriggerExit()
     {
         StartCoroutine("Close");
     }    

OnMouseUp, it tells me I am not near enough to the mob, basically saying that myGuiScript is null, even though per my other debug.log, I AM inside the collider. Then I also get a null reference for one of the myguiscript.whatever in the coroutines.

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

3 Replies

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

Answer by madmike6537 · Feb 25, 2013 at 12:42 AM

Ended up getting this working. It was another "mob" walking around screwing up my trigger, DOH! :)

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

Answer by jogo13 · Feb 07, 2013 at 12:56 AM

Try adding 'gameObject' after 'transform':

 myGuiScript = col.transform.gameObject.GetComponent<myGUI>();

(This is related to the component null 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 madmike6537 · Feb 07, 2013 at 01:40 AM 0
Share

Hmm will try also thanks for reply.

avatar image
1

Answer by Kergal · Feb 07, 2013 at 01:03 AM

won't help ;)

but I think I found a solution ;) - according to the API for OnTriggerEnter :

Note that trigger events are only sent if one of the colliders also has a rigidbody attached. !

if that is what you need. I tried it and my objects can now access the script.

Comment
Add comment · Show 2 · 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 madmike6537 · Feb 07, 2013 at 01:39 AM 0
Share

Hmm ok will give this a try. $$anonymous$$y character actually has a rigid body on it. Does it matter which object the rb is on? I don't believe it does. But you say this code works for you? Hmm strange.

avatar image madmike6537 · Feb 09, 2013 at 03:06 AM 0
Share

Well I double checked and I definitely have a RB on my character. Was there any differences in how you set up the code for why it might not be working on my end?

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

11 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

Related Questions

Is there a way to find other's components using OnTriggerEnter? 1 Answer

Collection Using "GetComponent" 1 Answer

How can I increase Health Value on trigger enter, to another game object? 3 Answers

C# - Problem with trigger that won't activate 1 Answer

getComponent not working with Triggers? 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