Hey all, just having a simple trigger issue for a UI element and not sure why it is not working. I am making a square with a sphere trigger collider that, when entered, will turn a UI element on, and when exited, will turn the UI element off. However, I am having some issues. When entering the sphere trigger, the UI element turns on as needed. However, when exited, the UI element stays on screen and will not leave. Any thoughts as to why this could be happening? Code is below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoxCollision : MonoBehaviour
{
public GameObject boxDisplay;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
boxDisplay.SetActive(true);
}
}
void OnTriggerExit(Collider other)
{
if(other.tag == "Player")
{
boxDisplay.SetActive(false);
}
}
}
Tested it out, and it does work, but only extremely selectively. I tried switching it out with another box collider which works slightly better. Guess its just a case of wonky detection??? However, still does not work to my liking. It isnt overly complex too, just simple models. Not sure why this is happening lol
I would take it even further and say make sure it runs OUTSIDE of the if statement… print the tag of the collider and see what it might be right at the top of those functions.
Also, if you put Debug.Log() calls inside each of those trigger functions, right at the top, and print the name and the tag of the collider involved, that might give some clues too.
Oh … how are you moving the object? You can NOT just assign to transform.position (and you can’t use Transform.Translate either!).
You can only move Rigidbody physics by calling the .MovePosition() method on the actual rigidbody. Otherwise yeah, you are startling the physics system and sneaking stuff around it.