[HELP] List instance not being destroyed OnTriggerExit
I have tried many other solutions but yet not success, I'm quite a beginner and wondering If I could receive some help :)
[code=CSharp] using System.Collections; using System.Collections.Generic; using UnityEngine;
public class LinkTest { public GameObject link; public Connector connector; public string targetName; }
public class Detector : MonoBehaviour { public GameObject linkPrefab;
private List<LinkTest> linksList = new List<LinkTest>();
void OnTriggerEnter(Collider co)
{
if (co.gameObject.tag == "Enemy")
{
if (linkPrefab != null)
{
LinkTest newLink = new LinkTest() { link = Instantiate(linkPrefab) as GameObject };
newLink.connector = newLink.link.GetComponent<Connector>();
newLink.targetName = co.name;
linksList.Add(newLink);
if (newLink.connector != null)
newLink.connector.MakeConnection(transform.position, co.transform.position);
}
}
}
void OnTriggerStay(Collider co)
{
if (linksList.Count > 0)
{
for (int i = 0; i < linksList.Count; i++)
{
if (co.name == linksList[i].targetName)
linksList[i].connector.MakeConnection(transform.position, co.transform.position);
// Everytime an enemy is outside the trigger deletes and remove the line renderer
}
}
}
void OnTriggerExit(Collider co)
{
if (linksList.Count > 0)
{
for (int i = 0; i < linksList.Count; i++)
{
if (co.name == linksList[i].targetName)
Destroy(linksList[i].link);
}
}
}
} [/code]
There is no errors or warning so I'm quite confused why it isn't working..
Answer by unity_BUJQdslYm7EuFQ · Jul 15, 2021 at 02:03 PM
Try something like this:
void OnTriggerExit(Collider collider)
{
if (linksList.Count > 0)
{
for (int i = 0; i < linksList.Count; i++)
{
if (collider.name == linksList[i].targetName)
{
Destroy(collider.gameObject);
}
}
}
}
Answer by dzvera951 · Jul 17, 2021 at 05:36 PM
@unity_BUJQdslYm7EuFQ Weirdly enough it still isn't working.
Your answer
Follow this Question
Related Questions
2D Arrays Help. Why to use 2D arrays for 2 ints 2 Answers
How Do I Create a List of Two Strings? C# 1 Answer
[C#] Enum not working properly 2 Answers
Unintelligible errors in the console 1 Answer
Updating a List from a Custom Inspector 0 Answers