Question by
thecaesar · Nov 07, 2019 at 12:54 AM ·
gameobjectgetgameobject.name
How can i get the gameobject name which contain the script
I wants to make check : if the name of the gameobject that contain the script is army do some thing and if name is enemy do another thing
Comment
Best Answer
Answer by dacarrera · Nov 07, 2019 at 01:33 AM
You can access a gameObject's name with gameObject.name
. That is, if your sample.cs script is attached to a gameobject called 'army', gameObject.name
will return army
A basic implementation could look like:
if(gameObject.name == "army")
{
// do something here if the game object's name is army
}
else if (gameObject.name == "enemy")
{
// do something here if the game object's name is enemy
}
Put this inside a function and you'll be good to go!
Your answer
Follow this Question
Related Questions
SetActive is just working if i press twice in the first time 2 Answers
Have a gameobject switch its values? 0 Answers
How can i Instantiate gameobject in array 0 Answers
How can I put buttons on a Cube? 0 Answers