Collider Trigger Event Methods(Enter, Exit, Stay) in Unity Game Engine


TriggerEvents.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TriggerEvents : MonoBehaviour
{
    void OnTriggerEnter(Collider other)
    {
        if(other.gameObject.name == "Cylinder")
        {
            Debug.Log("Enter");
            gameObject.GetComponent<Renderer>().material.SetColor("_Color", Color.red);
            other.gameObject.GetComponent<Renderer>().material.SetColor("_Color", Color.blue);
        }
    }

    void OnTriggerExit(Collider other)
    {
        if(other.gameObject.name == "Cylinder")
        {
            Debug.Log("Exit");
            gameObject.GetComponent<Renderer>().material.SetColor("_Color", Color.green);
            other.gameObject.GetComponent<Renderer>().material.SetColor("_Color", Color.white);
        }
    }

    void OnTriggerStay(Collider other)
    {
        if(other.gameObject.name == "Cylinder")
        {
            Debug.Log("Stay");
            transform.localScale += Vector3.one * Time.deltaTime;
        }
    }
}