- Home /
Get all GameObjects by variable value
I am making a level editor where player is inserting GameObjects into scene. Each GameObject is created with script that has public groupId variable. Player can change the groupId value to whatever int value he wants. The problem is, I don't know how to get the GameObjects with the same groupId to put them into ArrayList.
Answer by efeguclu · Jan 13, 2018 at 01:22 PM
you can use a Dictionary to make this first make an array with this gameobject.. then create a foreach loop and use :
//think that your Dictionary's var name is myDict and you groupId is in groupId script and variable is named as groupId and your gameobject array is sceneObjects[]
foreach(GameObject go in sceneObjects){
myDict.Add(go.GetComponent<groupId>().groupId,go);
}
then you can call your gameobject like this :
//34824 is groupId
myDict[34824]
Sorry I have never used dictionaries before. How can I create one ?
you use system.collections.generic and you can create like this :
private Dictionary<int,GameObject> sceneObjects = new Dictionary<int,GameObject>();
you should put objects in array to dictionary with its id
Answer by CrashCZ · Jan 13, 2018 at 04:59 PM
If I can have another question, How to add GameObject keys into List but only those with defined int value?
Your answer
Follow this Question
Related Questions
Problems with Arrays 2 Answers
List of bools assigned to each Gameobject in List/Array 0 Answers
Add to Array 1 Answer
Dynamic Array Generation via Inspector 1 Answer
How to Clone Array1 into Array2 in Unity? Using JavaScript. 0 Answers