- Home /
Getting the name of a gameobject, then using that name to find it.
Hello,
I'm looking to find out how to get the name of a gameobject then use that name to find that same gameobject.
The way things are set up in my game, the gameobject will go inactive at some point, so I need to find instantiated version of the gameobject and not the prefab. So to the best of my knowledge, I'm stuck using GameObject.Find.
So I have enemy1 with a script on it. (This script will be used on all the enemies).
enemy = GameObject.Find(enemy1); This works great.
However, when I put another copy of the enemy in the scene, it now is enemy1 (1). So the script will find enemy1, but not enemy1 (1).
So I'm wondering if there is a way to have the script find the name of the gameobject, then use that name to find the gameobject.
Something like...
gameObjectName = get.GameObject.name.
enemy = GameObject.Find(gameObjectName);
Thanks!
Answer by Acdia · Jul 19, 2020 at 07:43 AM
So you are looking for the GameObject the script is attached to, right? You can always get that gameobject using gameobject
So for example if you want to set it inactive use gameobject.SetActive(false)
Your answer
![](https://koobas.hobune.stream/wayback/20220613003412im_/https://answers.unity.com/themes/thub/images/avi.jpg)