- Home /
The question is answered, right answer was accepted
Can you check again something that is already in the IsTrigger collider ?
Okay so, I'm making a C4 planting system, and I'm in the last part where the C4 detects a destroyableWall, checks if it's time to destroy it, and if, destroys it.
The script works well, detects the wall and the other script finishes his work with timeToDestroyWall = true. The thing is, my C4 code won't work, and it' surely due to the fact that the checking is done only one time (it's not an Update() function and it's litteraly in the name, "OnTriggerEnter2D")
So, now you have a better understanding of my problem, here's the question :
What alternative can I use instead of OnTriggerEnter2D that would keeps checking items inside the collider ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class C4Manager : MonoBehaviour
{
public bool destroyableWall;
public GameObject Inventory;
public Inventory inventoryScript;
public void Start()
{
Inventory inventoryScript = Inventory.GetComponent<Inventory>();
}
public void OnTriggerEnter2D(Collider2D other)
{
if(other.CompareTag("destroyableWall"))
{
destroyableWall = true;
if (inventoryScript.timeToDestroyWall)
{
Debug.Log("attempting destruction...");
Destroy(other);
}
}
}
}
Answer by rage_co · Aug 01, 2021 at 12:20 PM
Like @thomaslijohnson said, you can try using OnTriggerStay2D().....or you could go with the approach that you create a list of colliders and add the current collider in OnTriggerEnter to thag list and remove it in OnTriggerExit and when destroying the object...this is a much more flexible approach....you can also put a part of this in OnTriggerStay which will check if the list already contains the collider through list.Contains(collider)...and if not...add it to the list....minimising the chance of an unwanted bug or glitch