- Home /
get name of child object script is attached to?
Hi, I'm trying to find the name of the current object that a script is attached to. I've got a hierarchy and a set of scripts, and I'm trying to use one script to send information to another game object. I've got all that working, but where I'm failing is where I'm trying to send the name of the current object that the script is attached to on with the SendMessage.
I've tried using "this.name" but it gives me the parent of the hierarchy... like in this example:
|-A
|-B
|-C
|-D
|-E
|-F
|-G
I've got my script attached to C, D and E, all sending different values, but when I use "this.name", it sends the name of "A" whereas I'd like to send [C,D or E] -- is there a way to do this without specifying the name of each one as a variable?
thanks in advance, Greg
Answer by duck · Jan 01, 2010 at 08:49 PM
You can use:
gameObject.name
To get the name of the GameObject to which the current instance of the script is attached. That should give you the "C", "D", or "E" for each instance of the script which is attached to those particular GameObjects.
Remember, 'this' represents the instance of the script itself, not the GameObject, however Monobehaviours have a 'gameObject' property, which you can use as shown above.
hi @Duck : Can I get the children's GameObject ins$$anonymous$$d of its child? I want to assign the child GameObjects to an array. I've been able to access each children's script. but not with the gameobject.. hope you can help me
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Changing gameobject assigned to a variable on input.getkey. 2 Answers
Converting some C# pseudo code to UnityScript.... 1 Answer
How do I stop an immediate collision with all objects from ocuring at the entry of game mode? 0 Answers