x


Refering to gameobject script is attached to

What is the best way in C# to refer to the gameobject that the particular script is attached to, IE, is there something similiar to "this"?

more ▼

asked Jan 27, 2012 at 02:28 PM

Teonnyn gravatar image

Teonnyn
147 121 780 146

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

1 answer: sort voted first

http://unity3d.com/support/documentation/ScriptReference/Component-gameObject.html

"this" is the script instance, not the Game Object to which the script instance is attached.

more ▼

answered Jan 27, 2012 at 04:14 PM

Jessy gravatar image

Jessy
18.2k 207 173 283

Therefore:

this.gameObject
Jan 27, 2012 at 04:53 PM Kryptos

page link broken

Jun 24, 2013 at 12:40 AM Compguru910

Yep, looks like the API links are broken again.

You have an API on your local machine. Find the Unity Install Directory :

C:/Program Files/Unity/Editor/Data/Documentation/Documentation/ScriptReference/Component-gameObject.html

Here's the details from that page :

Component.gameObject

var gameObject : GameObject

Description : The game object this component is attached to. A component is always attached to a game object.

C#

using UnityEngine; using System.Collections;

public class example : MonoBehaviour { void Example() { print(gameObject.name); } }

Jun 24, 2013 at 01:36 AM alucardj
(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:

x9346
x8751
x4754
x3438

asked: Jan 27, 2012 at 02:28 PM

Seen: 12367 times

Last Updated: Jun 24, 2013 at 01:36 AM