• 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
1
Question by GutoThomas · May 03, 2012 at 02:53 AM · instancemethodthis

Simple question about 'this' keyword

For example:

 myInstance.myMethod();

if I call the keyword this inside this myMethod it(`this`) will refer to myInstance or what?

I'm a bit confused about this.

Thanks from now!

Cheers!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by chainedlupine · May 03, 2012 at 03:24 AM

In C#: It will refer to the MonoBehaviour that is instantiated. ie: The component that lives in its container GameObject. So, yes, this would refer to myInstance, assuming myInstance was a reference to said component/MonoBehaviour.

In JS: Someone else will have to answer that, as I am not sure but it probably works the same. :)

Comment
Add comment · Show 1 · 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 GutoThomas · May 03, 2012 at 03:38 AM 0
Share

Thanks for both answers! Think was what I thought it was. To mark the correct one I'll use the criterion of who answered first, but thanks for both replies!

avatar image
5

Answer by Berenger · May 03, 2012 at 03:32 AM

The keyword this is indeed a reference to the object executing a member function. It's not necessary to use it in most cases, as that reference is used as well without the keyword. You should use when, for instance, a variable member of the class and a variable argument of a function member of that class have the same name. Maybe that code can make it clearer :

 private class MyObject
 {
     public string name = "No name yet";
     public void HelloTheWorld(){ print( "Hello, my name is " + this.name ); }
     // This function will do exactly the same
     public void HelloTheWorld2(){ print( "Hello, my name is " + name ); }
 }
 
 private class PutMeOnAGameObject : MonoBehaviour
 {
     private void Awake()
     {
         MyObject myObject = new MyObject();
         myObject.name = "John";
         myObject.HelloTheWorld(); // Hello, my name is John
         myObject.HelloTheWorld2(); // Hello, my name is John
     }
 }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to set different methods for instances of object 2 Answers

Get TouchScreenKeyboard instance from when clicking InputField on Mobile 2 Answers

Can someone help me with instance = this ? 0 Answers

Passing Parameters by Value or by Reference 2 Answers

Unserialized private variable values from static instance persist when exiting play mode 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