- Home /
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");
}
}
Did you try Debug logging? Like adding Debug.Log(col.gameobject.tag) before the if in OnCollistionEnter?
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.Send$$anonymous$$essage("Collide", Send$$anonymous$$essageOption.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.
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.
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.
Your answer
Follow this Question
Related Questions
Game Over GUI Question 2 Answers
Collider is only working on the first frame 0 Answers
Collision with renderer.enabled? 0 Answers
Find colliders after collision? 2 Answers
GUI Textures and Colliders 1 Answer