Home > other >  Display text in unity onTrigger
Display text in unity onTrigger

Time:01-28

I want to display a text in unity by a trigger, but it isnt working, this is my code. I have a collider with "Is Trigger" activated, the tags are ok, i dont know what its happening...

    public class LaptopTriggerCollider : MonoBehaviour
{
    public GameObject UiObject;
    public GameObject cube;

    void Start()
    {
        UiObject.SetActive(false);
    }
    void OnTriggerEnter(Collider other)
    {

        if (other.tag.Equals("player"))
        {
            UiObject.SetActive(true);
        }
    }
    void Update()
    {

    }
    void OnTriggerExit(Collider other)
    {
        UiObject.SetActive(false);
    }
}

CodePudding user response:

Firstly try to debug by putting a log statement in OnTriggerEnter function

void OnTriggerEnter(Collider other)
{
    Debug.Log("Is this even being triggered?");
    if (other.tag.Equals("player"))
    {
        UiObject.SetActive(true);
    }
}

I presume it won't print in console, reason could be that you are missing a rigidbody in the collision. A rigidbody is a must for collision or trigger events to be generated in Unity.

CodePudding user response:

Instead tag.Equals use CompareTag

if(other.CompareTag("player"))
{
   UiObject.SetActive(true);
}

CompareTag

  •  Tags:  
  • Related