how can I mark objects while instantiating?
hello, I instantiate 50 objects. I want to be able to differentiate them after this. how can I mark them individually? Thanks!
Answer by Cynikal · Sep 01, 2016 at 04:49 AM
Well, there are a few ways of doing this...
Add them to a List. Example:
public class Whatever{ public List<GameObject> MyList = new List<GameObject>(); private void MakeStuff() { //Do your loop or whatever here...and put this inside: GameObject go = Instantiate(OneOf50, blah, blah, blah); MyList.Add(go); } }
Option 2: is to use: int i = 1; GameObject go = Instantiate(OneOf50, blah, blah, blah); go.Name = "Whatever "+ i; i++;
That'll make your opjects with the name of: Whatever 1, Whatever 2, Whatever 3, etc.
The BEST is to use the List. As Option 2 is kind of sketchy, but you'd use: GameObject.Find("Whatever 3"); etc.
I have one more problem please give me an advice. :) @Cynikal
just my mistake it must be Gameobject.Find("example");
Answer by davit_goder · Sep 01, 2016 at 05:53 AM
so I did that but one more problem appeared. I check if distance between this object and example cube is 5 and object name is "example" my cube does not recognizes it. I tried just name but it still does not work.
GameObject ga_bact = (GameObject)Instantiate(bacteria, transform.position = new Vector3(x2, y2, 0), transform.rotation); ga_bact.name = "example";
than
if (Vector3.Distance(ga.transform.position, gameObject.transform.position) < 5 && ga.name=="example){
}