- Home /
getting the object that the running script is attached to
Sorry if this has been answered before but I'm not even sure how to word a search to find what I'm looking for.
Basically, I have 4 cubes on the screen, one is the player the other three are "enemies". Each enemy is running the same script since they will all behave the same way, but I'm trying to put into code how to get the name of the object that is currently running the script.
if I could write the code in english it would look like this
print(the name of the object in the hierarchy that is running this script);
Can anyone help?
Answer by qJake · May 05, 2010 at 12:59 AM
You're going to kick yourself when you find out. :P
print(gameObject.name);
Wouldn't that print the name of the component, not the game object?
thank you! that lower case 'g' I think is what was getting me
Well that's a program$$anonymous$$g concept right there... "GameObject" is the class name, and "gameObject" is the $$anonymous$$onoBehaviour field that points to a reference to "this" game object. Big difference. ;)
Your answer
Follow this Question
Related Questions
Find object that enters trigger 1 Answer
inside a script is there a way to disconnect a child from its paretn in c# 1 Answer
Load Object(3d model) by name with script at scene 2 Answers
End game then an Object is close to another object? 0 Answers
Why do I have lots of 'One Shot Audio' objects in my hierarchy? 1 Answer