- Home /
Getting component by name
I'm sure this has been asked before, but I've searched the answers and can't find what I am looking for, perhaps I am using the wrong terms. Basically I have a prefab I instantiate a bunch of times. In this prefab there is an empty object and under that there is a model, a script, and a bunch of empty game objects. The empty game objects each are named in a way that corresponds with how I intend to use them. Basically, from my script I am trying to get to the empty game object "objectA" which is a member of the same prefab as the script. I tried using GameObject.find("objectA"), but I am getting objects attached to other prefabs instead of the one on this instance. I could try GameObject.GetComponenet, but the component is just a game object and there are a bunch of them on the instance. Is there an easy way of getting a componenet by its name? I see a get component by tag, but I would really rather not start creating tags for each of these as seeing that with the way the game is structured I would eventually wind up with a lot of tags.
Answer by appearance · Feb 22, 2013 at 04:22 PM
You can use
GameObject.FindObjectOfType (typeof (myscript));
The problem is that the type I am looking for is GameObject, wich vague enough to return tons of things.