- Home /
Accessing one gameobject from many clones
Hi there, I've been lately playing around with game objects, and of course some questions appeared to which i will hopefully find answers. If let's say i have two game objects with two children each, and both main and children have same names (clones), how is it possible to access only one of them, let's say the second one ?
//Hierarchy illustration
Object
part1
part2
Object
part1
part2
For now i managed to code it working, but only if using one GO. Like this,
if(Input.GetKeyDown(KeyCode.X)){
GameObject.Find ("part1").SetActive(false);} //disables first GO in hierarchy with name part1, on a press of the button
As i understand, it is impossible doing this using gameobjects and .find. If we imagine that both of these objects are cubes with colliders attached, how would i disable (only on a press of the button) the one I'm colliding with? (but if im not colliding it wouldn't disable)
Thanks!
Answer by IgorAherne · Jul 21, 2013 at 12:01 PM
transform.GetChild(0) returns part 1 of object whose transform we are referring from this script (script is attached to the same object, that's why we can access the object's transform by typing "transform" keyword
if(Input.GetKey(KeyCode.X && transform.GetChild(0) != null)){ //if you are getting something from button and the child exists
transform.GetChild(0).SetActive(false) is what you want
}
Thank you for your answer, i understand what you mean there, but how would i access the objects transform dynamically (from collision lets say), because what if i have hundreds of these objects?
I prefer using transform.Find("part1")
ins$$anonymous$$d of getChild(0), since the order of children isn't always the same as in the Inspector. But they both search only children, and can be mixed with GO Find. See the docs, plus exs:
GameObject.Find("spot").transform.Find("part1")
or Transform p1 = collision.transform.Find("part1");
Legal to find something that doesn't exist, so could check if(p1!=null)
from the previous example.
Answer by Briksins · Jul 21, 2013 at 02:42 PM
As far as I understand your game objects are clones which u instantiate from prefabs right? And you trying to manipulate object from your avatar once you collide it right?
Then why don't you apply script logic on game object itself (prefab) instead of searching that object from your avatar? then you can set object trigger to fire event on colliding and check user input on object itself like this:
//pseudo-code = not real code, just logic:
if(collider == <player_avatr> && input == <some_key>)
{
//do some stuff
}
else
{
//I did collide with something else but not a player,
//or input didnt match
//do nothing
}
So instead of searching game object from player, let object it self detect collision and with whom it was collided
Thank you for an answer. I was thinking to try using trigger event on each item, but is this performance "friendly" way? Because if i would have 100 objects, there would be 100 trigger checks each frame event if the player is very far away from it. Hmm, or maybe i could disable the objects trigger checking if player is 50 meters far away, but then again, there's a checking going on about the distance.
Your answer
![](https://koobas.hobune.stream/wayback/20220613110536im_/https://answers.unity.com/themes/thub/images/avi.jpg)