- Home /
How can I call OnTriggerEnter2D from the object with the trigger on it?
So uh, I'm trying to make a one way platform, and it's proving to be difficult.
I'm trying to use a trigger object that goes underneath the platform, and when the player enters the trigger, it does the Physics2D.IgnoreCollision thing between the player's collider and the platform's collider.
The problem I'm having is that I'm trying to call OnTriggerEnter2D/Exit2D from a script on the trigger object, and I don't think that's working. Is there some solution to this?
Edit: I should have elaborated more. I need to figure out how to detect if the player has entered the trigger from the trigger object itself, not the player. Is that possible?
Did you select trigger on the collider? I seem to forget that once in a while.
Answer by spiceboy9994 · Nov 11, 2014 at 09:14 PM
In order to detect collisions, at least one of the elements colliding should have a RigidBody, in this case a RigidBody2D, usually you add this to your moving element (the player) as best practice. Does your player have this component attached?
Hope this helps
Yes, the player has a RigidBody2D. That's not the problem. I need to figure out how to detect if the player has entered the trigger from the trigger object itself, not the player. Is that possible?
Just curious, why do you need to detect the is trigger enter from the other object?, shouldn't be easier to handle that to the player and write something like this:
void OnTriggerEnter2D(Collider2D other)
{
/*All the player triggers here*/
/*my desired object trigger enter detection*/
if (other.gameObject.tag == "$$anonymous$$yOtherObjectSpecificTag")
{
/*Execute functions*/
}
}
If there's an scenario where you need to execute other function on the external object, you can always use GetComponent, so it should look like this
void OnTriggerEnter2D(Collider2D other)
{
/*All the player triggers here*/
/*my desired object trigger enter detection*/
if (other.gameObject.tag == "$$anonymous$$yOtherObjectSpecificTag")
{
/*Execute functions*/
/*Find External component*/
var externalComponent = other.gameObject.GetComponent<$$anonymous$$yExternalComponentType>();
/*Execute external component functions*/
externalComponent.publicFunction();
}
}
Just let me know if that helps
@spiceboy9994: Regarding your suggestion in your comment: No it's not easier to handle such things in the player. That's not the responsibility of the player. If you would go by this logic you would have to put everything in the player when the player can interact with it.
It's usually easier to have a small script on the object itself. The purpose of an object should be reflected in a script on that object. For example coins that can be picked up would have a script that will check if the player collides with it (using OnTriggerEnter(2D)).
Answer by Bunny83 · Nov 13, 2014 at 07:05 PM
Trigger messages are always sent to both objects involved. If your platform trigger is a static trigger collider it will receive an OnTriggerEnter2D event when the player objects enters the trigger.
However, If your platform collider is a child of another rigidbody object, the rigidbody will receive the event and not the child collider. This is because a rigidbody will "collect" all colliders which are a child of itself and use it as compound collider.
You would need to attach a kinematic rigidbody to your trigger object if you want to receive the message there in that case.
You might want to take a look at:
Your answer
![](https://koobas.hobune.stream/wayback/20220613171257im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Platform Control 1 Answer
How to use rigidbody.MovePosition within OnTrigger? 0 Answers
How do I switch control values? 1 Answer
OnTriggerExit() help 0 Answers