Hi All, I’ve encountered a little problem lately. So I have a whole bunch of gameobjects and they all have the same script attached to them and there is the same int in the script but the int is different the gameobjects.
So I was trying to find a way to store all the gameobjects and their own different int in a dictionary, so for example, if I pick a int, I would find the gameobject that it is appointed to the int.
Anyway of doing this? I would really like to know. Thankyou. (I am writing in javascript)
Dictionary is one way to go if the int values are not all consecutive. In this case, considering it goes from 0 to n and never skip a value in between, you can just use an array.
You could also use an ordered list, that is if you consider iterating through the collection based on the int value.
But if you are about to get any value, even negative, and you need to find based on the int value, then dictionary is the one.
var dict = new Dictionary.<int, GameObject>();
var objs = FindObjectsOfType(ScriptName);
foreach(var item in objs){
dict.Add(item.intValue,item.gameObject);
}
and use as :
if(dict.Contains(10)){
var obj : GameObject = dict[10];
}