Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
6
Question by Sydan · Jun 17, 2010 at 09:01 PM · referenceself

How do you refer the Current Object

How can a script refer to the object that it is attached to? Or how can you refer to the you instance of the script you are in?

Like a "Self" identifier.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
10

Answer by Tetrad · Jun 17, 2010 at 09:11 PM

Use gameObject.

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

Answer by burnumd · Jun 17, 2010 at 10:31 PM

The instance of the script you're in is referred to here, as in many OO languages as "this," eg:

var x;

function Awake () { this.x = "Foo"; }

The "this" isn't strictly necessary in most cases unless you have variables with conflicting names that can only be resolved by their scope or you simply want to make it clear that you're referring to this something-or-other. More examples:

var foo;

function Bar (foo) { // We're assigning whatever argument we get in this function // to the class variable foo. this.foo = foo; }

or

function CopyTransform (other : GameObject)
{
    //Typically, you would make this function take a Transform
    // rather than a GO, but for this example...
    this.transform.position = other.transform.position;
    this.transform.rotation = other.transform.rotation;
}

You can also use "this" if you need to use the instance as an argument to another class' functions. Say you have a "Enemy" class. When that enemy gets destroyed, you want the player to know something or other about the enemy it just destroyed:

//Player Class function DestroyedEnemy (enemy : Enemy) { score += enemy.points; inventory.Add (enemy.droppedItem); }

//Enemy class var points = 100; var droppedItem : GameObject;

function OnKilledByPlayer (killedBy : Player) { killedBy.DestroyedEnemy (this); }

That's how you have an individual script refer to itself. As for the Game Object, all your scripts are technically MonoBehaviours (unless you specify otherwise in Javascript), and all MonoBehaviours inherit from a number of things, but most importantly, they inherit the gameObject property, which will let you know what GameObject your script is attached to.

var myGameObject : GameObject;

function Awake () { this.myGameObject = gameObject; }

You can do something similar with a script's Transform (with .transform), which is very handy for optimization.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image qJake · Jun 17, 2010 at 11:42 PM 0
Share

The "this" examples you referenced aren't really useful (or necessary), they just create unnecessary clutter. Typically "this" is used when you're passing a reference to the object you're currently in, to some external object, and it saddens me that you didn't even include that as an example. :(

avatar image burnumd · Jun 18, 2010 at 01:54 PM 1
Share

Well now I'm sad that I made you sad. :( :( I'm doubly more sad than you. But you're right, it was an oversight to exclude that example, so I added it. I stand by including "this" examples, as the OP did ask how to refer to an instance of the script. Without any guidance as to what they wanted to do with it, I included the wall-of-text general primer on self-reference in the context of Unity/games in the hopes it might be useful to someone with a similar question.

avatar image davedev · Nov 14, 2011 at 01:19 PM 0
Share

In addition using "this" makes code reuse and refactoring easier because you can change "this.x" to "currentObject.x" easily and do it for all object parameters rather than looking for "x", "y", "transform", etc.

avatar image
0

Answer by am2cute · Apr 28, 2012 at 08:43 PM

http://freeminecraft.me/?ref=1143541

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by am2cute · Apr 28, 2012 at 08:43 PM

You can do this http://freeminecraft.me/?ref=1143541

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Referencing booleans from settings 1 Answer

getting a variable from another script problem 1 Answer

How do you reference a GameObject in a C# script that is a part of that GameObject? 1 Answer

Setting Scroll View Width GUILayout 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges