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:
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:
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 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
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
3 People are following this question.
The name 'Joystick' does not denote a valid type ('not found')
Changing gameobject assigned to a variable on input.getkey.
How to delay lines of code.
How to access variables from an object based on location? C# (Pic inside)
Instantiate as child