How do I reset a trigger after moving an object out of it?
Hello! I have a small problem with a trigger and bool. The small square has a trigger collider on it. When I push one of the buttons on the UI, an object is instantiated. I have set it up so that if I drag on the object with a mouse, the object moves away from the trigger. My problem is that the “is empty” bool doesn’t reset after I move the object away from the trigger. If I put an “is empty = true” in the update, then, of course, it’s always true and I can spawn multiple objects at the same time.
https://imageshack.com/i/pmvak8Ndp
I’ve tried the OnTrigger Enter/Exit combo and OnTriggerStay as well. I’m sure OnTriggerStay is probably the answer(?), but I think I’m missing some kind of logic or placement to ensure that the bool resets properly. I’m wondering if it’s because I put the same script on all of the buttons or because it’s not Instanced? I would appreciate any tips about what direction I should be taking. Thanks in advance!
using UnityEngine; using UnityEngine.UI;
public class InstantiateObjects : MonoBehaviour {
public GameObject goPrefab;
//list
public List<GameObject> createdObjects = new List<GameObject>();
//drop area
public GameObject dropZone;
public bool dropZoneEmpty;
//game object limits
public int gameObjectLimit;
public int listSize;
// Start is called before the first frame update
void Start()
{
dropZoneEmpty = true;
}
private void Update()
{
listSize = createdObjects.Count;
}
// Update is called once per frame
public void CreateObject()
{
if (dropZoneEmpty == true && listSize < gameObjectLimit)
{
Vector3 position = dropZone.transform.position;
//instantiate object
GameObject go = (GameObject)Instantiate(goPrefab, position, Quaternion.identity);
createdObjects.Add(go);
dropZoneEmpty = false;
}
}
}
Your answer
