- Home /
Converting a GameObject to a class?
What I am trying to do is get an array of a class, from calling GameObject.FindGameObjectsWithTag()
Basically this:
PlaceVariables[] holes = GameObject.FindGameObjectsWithTag("hole");
Obviously this doesn't work, but I dont know how to do it right
Do you mean that you have PlaceVariables
scripts on the game objects with the tag "hole"?
Answer by Cains · Nov 04, 2013 at 12:55 PM
Considering you want the PlaceVariables components from all the GameObjects, you'll need an array of GameObjects to store the data from FindGameObjectsWithTag and an array of PlaceVariables classes. Then you can just populate the array of PlaceVariables by iterating through the array of GameObjects.
GameObject[] gos = GameObject.FindGameObjectsWithTag("hole");
PlaceVariables[] holes = new PlaceVariables[gos.Length];
for (int i = 0;i < gos.Length;i++) {
holes[i] = gos[i].GetComponent(PlaceVariables);
}
Don't have a chance to test it (and I don't often code in C#), but I think it'll work.
Your answer
Follow this Question
Related Questions
Spawning GameObjects with help of classes --- Attaching classes to GameObjects 1 Answer
Can someone help me find the closest waypoint to a player? 1 Answer
How to get if all all objects are destroyed to start another wave 1 Answer
Cannot convert object to gameObject 3 Answers
Cannot convert type `UnityEngine.GameObject' to `UnityEngine.Plane' 1 Answer