- Home /
Question by
abraamraafat99 · Mar 27, 2020 at 10:26 PM ·
collisionupdateontriggerentercollision2denable
so how to OnTriggerEnter2D(Collider2D collision) work update so game component if player do not enter collision so game component enabled again
using System.Collections; using System.Collections.Generic; using UnityEngine; public class GravityAndFall : MonoBehaviour { private void Update() {
}
public void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == "Player")
{
collision.gameObject.GetComponent<game>().enabled = false;
}
else if(collision.gameObject.tag == "Player")
{
collision.gameObject.GetComponent<game>().enabled = true;
}
}
}
Comment
Answer by Adamcbrz · Mar 28, 2020 at 02:23 AM
This question is very unclear but I am going to take a stab at interpreting it. I think the question is "How to use the OnTriggerEnter2D Unity Event to disable a object when it enters and re-enable it when it leaves."
To do that you would have to implement the OnTriggerExit2D(Collider2d collier) method.
void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == "Player")
{
collision.gameObject.GetComponent<game>().enabled = false;
}
}
void OnTriggerExit2D(Collider2D collision)
{
if(collisoin.gameObject.tag == "Player")
{
collision.gameObject.GetComponent<game>().enabled = true;
}
}
I hope that answers your question. If not please clearify so we can understand better.