- Home /
My script isn't detecting OnTriggerEnter why?
I am trying to get my car to update which lane its in through tags. I have large cubes that are triggers with the tags Lane1-6 for some reason its not detecting the collision. The car is already in a trigger box so I can try to detect it. I have checked each cube and all of them have Is Trigger set to true. I haven't set any motion to the car yet, but that shouldn't effect anything right?
Here is what I am using to detect the trigger:
void OnTriggerEnter(Collider Hit)
{
switch (Hit.tag)
{
case "Lane1":
Debug.Log("Lane 1");
Lane = 1;
break;
case "Lane2":
Debug.Log("Lane 2");
Lane = 2;
break;
case "Lane3":
Debug.Log("Lane 3");
Lane = 3;
break;
case "Lane4":
Debug.Log("Lane 4");
Lane = 4;
break;
case "Lane5":
Debug.Log("Lane 5");
Lane = 5;
break;
case "Lane6":
Debug.Log("Lane 6");
Lane = 6;
break;
}
if (Hit == null)
{
FindLane();
}
if (Hit.tag == "Cleaner")
{
GameObject.Destroy(gameObject);
}
}
This is going before the Start and Update functions.
Sorry I forgot to mention I tried moving it around though the scene view and nothing happened. Should I try to work out the movement before the lane detection?
Answer by Dave-Carlile · Oct 25, 2013 at 03:03 PM
According to the OnTriggerEnter documentation, the trigger event happens when an object enters the trigger area. Someone please correct me if I'm wrong, but I don't believe it will happen if the object is already inside the trigger area.
Edit: I'll correct myself - the event is called even if the object starts out inside the trigger. Make sure at least one of the objects has a rigidbody
component attached.
Your answer
Follow this Question
Related Questions
Undefind tag? 0 Answers
Making Sprite Disappear on Trigger 2 Answers
Trouble with light switch 3 Answers