- Home /
GameObject.find and positioning
Hello, I am instantiating objects at run-time and i'm naming them i+"empty" , where i is the variable for the cycle that is instantiating the objects, and when I try to find those objects in another cycle and set them active like this:
for (int i = 0; i<cardList.Count; i++) {
GameObject emp;
emp = GameObject.Find(i+"empty") as GameObject;
emp.SetActive(true);
}
When I run the project I get a "Object reference not set to an instance of an object." error, what am I doing wrong?
Another question, take the same object as above, how can I position hit so it gets the same position across the various sizes of monitors/resolutions ? I'm actually positioning the objects with their pixel inset components but I want to position them with the transform component.
Thanks in advance
We need a lot more information. Please give us the following:
The entire error message as presented by Unity, not your paraphrase thereof.
A screen shot of the hierarchy window at the time this code attempts to run.
Problem was that I was tryinh to find an inactive object... I still need help with the positioning though
Answer by rutter · May 21, 2014 at 07:41 PM
GameObject.Find only finds active GameObjects, unfortunately.
Remember, though, that Instantiate returns a reference to the newly created object:
//assumes "empPrefab" is set elsewhere
GameObject emp = Instantiate(empPrefab) as GameObject;
You could keep that reference, perhaps in an array, list, dictionary, or some other collection. See the Unity wiki article Which Kind Of Array Or Collection Should I Use?
Guess the above answer says all, thanks! Is tagging the objects the only way to .Find it?
And I still need some help with the positioning issue, please.
Thanks again for the fast answers!