Detect multiple gamobjects
In my project I'm trying to use a large cube to detect multiple smaller cubes inside of the large cube so that I can run the smaller cubes through a foreach loop to separate them by characteristics, So now I'm wondering how I would go about storing those objects inorder to run them through the foreach loop. If this isn't clear enough I will elaborate, Thanks for your time.
-Bazzalisk
Answer by DFT-Games · Oct 29, 2015 at 01:25 AM
I don't think it's necessary to parse the objects to categorize them, you can just do something like this in a script attached to the big trigger cube. Here I assumed that you created the following class members:
private List<GameObject> list1 = new List<GameObject>();
private List<GameObject> list2 = new List<GameObject>();
private List<GameObject> list3 = new List<GameObject>();
And then in the script you add the two Unity handlers:
public void OnTriggerExit(Collider other)
{
if (other.CompareTag("Some Tag"))
list1.Add(other.gameObject);
else if (other.CompareTag("Some other Tag"))
list2.Add(other.gameObject);
else if (other.CompareTag("Some more Tag"))
list3.Add(other.gameObject);
}
public void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Some Tag"))
list1.Remove(other.gameObject);
else if (other.CompareTag("Some other Tag"))
list2.Remove(other.gameObject);
else if (other.CompareTag("Some more Tag"))
list3.Remove(other.gameObject);
}
Hope this helps ;)
-Pino
Your answer
Follow this Question
Related Questions
I guys I have a really hard question 0 Answers
How Do I Create a List of Two Strings? C# 1 Answer
How to remove flashing screen when changing Canvas Render Modes 0 Answers
List keeps losing items. 0 Answers