- Home /
Other
OnTriggerEnter will not work in unity 5
void OnTriggerEnter(Collider other)
{
Debug.Log(other);
}
other would be my player which only have a rigidbody and and box collider on it . the object with the trigger set to active is just a simple box with this script on it .
how can i get OnTriggerEnter to work
Both objects need a collider(is trigger) and at least one of them a rigidbody component.
Answer by tanoshimi · Apr 12, 2015 at 01:01 PM
Both objects involved in a collision need to have colliders. So, the "just a simple box" to which this script is attached needs to have a collider component as well.
For OnTriggerEnter to function, the Collider on the object with which the script is attached must be checked "IsTrigger".
both of them have colliders. what else could be wrong ?
I think it is working already its just that you have it debugging out other, which is a collider..Its doing that but not showing you anything..Try (Debug.Log(other.name)
$$anonymous$$ake sure the other collider is not a trigger, a trigger won't trigger a trigger by default
It might help if you could post a screenshot of the inspector panels for the two objects in question - it's a bit hard to follow exactly what setup you have: e.g. it sounds like you have:
Object A (Box): - Box Collider (marked as IsTrigger) - This script
Object B (Player): - Rigidbody (is kinematic?) - Box Collider
If so, can you try attaching this script to the player object, not to the box?
sorry for the very late reply , I no longer have the project but I will test it out again later on
Answer by anthodb · Jun 18, 2015 at 04:14 PM
I had same issue and I had to set both of my colliding Gameobjects on the "CollisionLayer". Then it worked ;) I don't know if this is a new feature of Unity5 but I can tell I spent most of the day searching for a solution.. Hope that helps..
Answer by lightgem12 · Apr 12, 2015 at 05:44 PM
um.. I'm abit new to this so i may be wrong but when you use debug.log you may need quotations. Like
void OnTriggerEnter2D(Collider2D other)//if you are using 3D you dont need the 2D part {
Debug.log ("Hi There"); }
Indeed "" is used to indicate a Literal String (Text).
You can combine items in Debug.Log
Debug.Log("$$anonymous$$y int value is " + myInt.ToString());
As OP used a variable name the "" are not needed even if that variable is a String.
String myString = "THIS IS A STRING";
void Update()
{
Debug.Log("This is a string and " + myString);
}
Dont worry, not going to downvote you ;)
JUST SAYING....I NEVER DOWN VOTE ANYON$$anonymous$$ why do i have to be saying this . Unity needs to show the name of persons who down vote so that we can contact them and ask for their corrections and ideas.
Answer by meat5000 · Apr 12, 2015 at 05:52 PM
What is this?
Debug.Log(other);
I'm quite sure you can't output a Collider to the Console.
Did you want
other.gameObject.name
instead?
if I remember correctly it will output "UnityEngine(Object)". I tried accessing and setting the value of a public variable in a script attached to "other" however no change was made
Can you be sure that it functioned as intended? Does that routine work if you uncheck IsTrigger and Use OnCollisionEnter?
OnCollisionEnter works just fine , but nothing happens once I check isTrigger
Answer by SnStarr · Apr 12, 2015 at 12:49 PM
Use the new Unity UI Event Systems. Include Using.UnityEngine.EventSystems at the top and make sure that your class inherits from IPointerEnterHandler.
Then set up an Event System on the Player and use the following script in your code attached to it.
void OnPointerEnter(PointerEventData pointerData)
{
}
Hi, thanks for the help ,however that is not what i am referring to .
weirdly i have the same issue. i have a cylinder with a capsule collider and a rigidbody. and a box right in front of it(yes with a box collider and is trigger on). when it passes through neiter onCollisionEnter nor onTriggerEnter goes off. i cant figure out what i have different then the other 300 times that i used this. it feels like a bug.
ok i just opend a fresh project and redid basically this tutorial http://unity3d.com/learn/tutorials/modules/beginner/physics/colliders-as-triggers?playlist=17120 and it doesnt work.
lol dammit....learning curve ay! thanks for pointing that out