x


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

more ▼

asked Jan 01, 2010 at 07:53 PM

Greg Dunn gravatar image

Greg Dunn
14 1 2 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jan 01, 2010 at 08:49 PM

duck gravatar image

duck ♦♦
48.2k 137 266 514

hi @Duck : Can I get the children's GameObject instead 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

Sep 08, 2011 at 03:33 AM beco13
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x9515
x3760

asked: Jan 01, 2010 at 07:53 PM

Seen: 5273 times

Last Updated: May 24 at 03:32 PM