- Home /
Gets object with a script
What's i need is to get a gameobject with a script but it can't be GameObject.Find it is possible?
You need to explain more, why can't you use GameObject.Find?
Can you hold a public reference to the object in the script and just drag the object onto the slot in the script?
Is the object being instantiated at runtime?
Give me some more information and I'll see what I can come up with.
Answer by adsamcik · Jan 17, 2015 at 07:12 PM
Yes, there are multiple ways of getting GameObjects.
You can use transform.Find() which looks for transforms on his children objects. If I am not mistaken, it must be direct child.
Different approach would be GetComponentInChildren(). It looks for specific components, it might be desired if object you are looking for uses unique script or if you need to get all the objects with that script. As the name states it looks for the objects in children not in entire hierarchy and I think it doesnt look in direct children only. There is also GetComponentsInChildren() which returns array of all these objects.
Or if it is possible I would recommend making the variable public and setting it in inspector, because it is definitely the fastest way of looking for an object and you can be sure it found the right one.
Those require you have the parent object. FindObject(s)OfType if you do not have it and you want to search by script type or FindGameObjectsWithTag/FindWithTag if you want to look by tag.
Your answer
