- Home /
Instantiated Objects dont get their Script referenced properly
I have the problem that all of my Instantiated Objects dont get their Script referenced properly from another Object because they're name isn't ,,Sprawler" anymore but ,,Sprawler(Clone)" . My Question would be how can i reference the Script of my Clones properly. I just need to call a function later on from that referenced script.
SprawlerEnemyAI = GameObject.Find ("Sprawler").GetComponent ();
This works for my manual placed Object, but changing the Find name to Sprawler(Clone) will make the Function not beeing called. Even though MonoDevelop autocorrects to the Function, meaning that somehow i do reference it right... its just not getting called
If you must use the name for reference, after instantiating your Sprawler, just change it's gameobject.name back to Sprawler.
Answer by jmgek · Nov 15, 2016 at 09:48 PM
There are a few ways, renaming your clone in it's constructor this.gameobject.name = "Sprawler"; Or changing your find to ("Sprawler(clone)");
BUT I would suggest putting your SprawlerEnemyAI into a list when created:
you don't have to rely on GameObject.Find() and it's expense.
You have full references and are not limited by the hard coded name you are using.
It's a better practice since you are already running into problems. "I can't rename objects freely, I can't change my scene objects without breaking this script. I don't know if these objects actually exist under those names. "
Answer by roman_sedition · Nov 16, 2016 at 07:27 AM
I agree, you should get into the practice of adding your gameobjects into a list should you ever need to be able to reference that object.
GameObject someSprawler = Instantiate(Sprawler);
sprawlerList.Add(someSprawler);