• 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
Question by NefonixGames · May 04, 2013 at 03:26 PM · javascriptcollisionguicollider

Problems with OnTriggerEnter

Ok so anyway I am trying to create a script where when the player enters an area a gui pops up and he takes damage like a radiation zone to stop him from going out of the map. Heres my script don't really know what's wrong with it. and by the way the character is a cube and the area i am entering is also a cube just without the mesh render with is trigger selected under box collider!

 var showgui : boolean = false;
 public var myskin : GUISkin;
 function OnCollisionEnter (col : Collision) {
 if(col.gameObject.Tag == "Player"){
 PlayerStats.curHp -= 100;
 showgui = true;
 }
 }
 function OnGUI ()
 {
     GUI.skin = myskin;
     if (showgui)
     {
         GUILayout.Box("WARNING ENTERING RADIATION ZONE");
     }
 }
 
Comment

People who like this

0 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 ExTheSea · May 04, 2013 at 03:31 PM 0
Share

Did you try Debug logging? Like adding Debug.Log(col.gameobject.tag) before the if in OnCollistionEnter?

avatar image NefonixGames · May 04, 2013 at 03:42 PM 0
Share

Nope that didn't work either:(

avatar image ExTheSea · May 04, 2013 at 04:04 PM 0
Share

So the debug log wasn't called? Is the gameobject this is attached to not moving? ...because in this case OnCollisionEnter won't get called as the object isn't actually "entering" a collider. One solution you have is to let the player handle the collision with something like this

 function OnCollisionEnter (col : Collision){
   col.gameobject.SendMessage("Collide", SendMessageOption.DontRequiereReceiver);
 }

This will call the function Collide on every gameoject it collides with. If you can specify it to several tags you can add an if before the sendmessage cal to propably avoid performance problems.

avatar image NefonixGames · May 04, 2013 at 05:26 PM 0
Share

Not really sure how I would do this and my space ship is moving it is a cube with a click to move script and a main camera that follows it. so yeah if you could explain more sorry kind of a noob here/ more like give me the script if you can.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by DaveA · May 04, 2013 at 05:46 PM

Make sure your player cube has a RigidBody on it. Make sure the above script is on the cube being entered, not on the cube that is moving. Put the following after 'showgui = true;':

Debug.Log ('radiation zone entered');

and see if it ever shows in the console. Also read the manual about using the Mono debugger. It's really important to learn to use that.

Comment

People who like this

0 Show 0 · 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.

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

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

OnCollisionEnter Collision not detected? 1 Answer

How do I pass through certain colliders but detect all collisions? 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Need Help Calling A Trigger Function 1 Answer

OnMouseOver parenting problem 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