Hi, I have certain Items inside a collider, and all the items inside the collider are taken inside a list. But when I take an item outside the collider (And destroying it). It gives me an error, about still trying accesing the item when the item is destroyed.
I believe the error is in the part of “OnTriggerExit”, So you don’t have to read the entire code
The error is (I think) that the item is not removed from the list.
This is my code:
public class ActivateTextMeshes : MonoBehaviour
{
public List textMeshes = new List();
void OnTriggerEnter(Collider other)
{
if (other.GetComponentInChildren<TextMesh>() && other.gameObject.tag == "PickableItem")
{
/* ADDS ALL TEXT MESHES TO LIST */
if (!textMeshes.Contains(other.gameObject.GetComponent<TextMesh>()))
{
textMeshes.Add(other.gameObject.GetComponentInChildren<TextMesh>());
}
}
/* CHECKS IF THE PLAYER IS INSIDE THE COLLIDER */
if (other.GetComponent<Collider>().bounds.Contains(player.transform.position))
{
ActivateAllTextMeshes();
}
}
void OnTriggerExit(Collider other)
{
if (!other.GetComponentInChildren<TextMesh>() && other.gameObject.tag == "PickableItem")
{
textMeshes.Remove(other.gameObject.GetComponentInChildren<TextMesh>());
textMeshes.TrimExcess();
}
//If the player exits from the collider
if (other.GetComponent<Collider>().bounds.Contains(player.transform.position))
{
DeactivateAllTextMeshes();
}
}
void ActivateAllTextMeshes()
{
for (int i = 0; i < textMeshes.Count; i++)
{
textMeshes*.GetComponent<TextMesh>().gameObject.SetActive(true);*
}
}
void DeactivateAllTextMeshes()
{
for (int i = 0; i < textMeshes.Count; i++)
{
textMeshes*.GetComponent().gameObject.SetActive(false);*
}
}
}