• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by SirMalarkey · Mar 13, 2013 at 08:58 PM · c#

how would i write this in unity script?

 public CharacterController CharacterController { get { return m_Controller; } }

i'm quite good with unity script but i have no idea when it comes to C# could anybody translate this little tidbit for me?

thanks, Malarkey

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

3 Replies

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

Answer by hoy_smallfry · Mar 13, 2013 at 11:39 PM

What you are looking at is a C# property. They are a language construct built into C# for making getting and setting values easier. In that sense, they are halfway between being variables and functions.

See, in other C-style languages, you have the public, private, and protected keywords to describe what kind of access other classes or entities have to a class' members.

For example:

 class X
 {
      public int a;

      public X()
      {
          a = 20;
      }
 }
 
 class Y
 {
     void DoSomething(X x)
     {
          x.a = 3; // ALLOWED
          Debug.Log(x.a) // ALLOWED, prints out 3
     }
 }

As you can see, class Y can access class X's a member because it's declared as public. If it was marked private, you'd get a compile-time error. The downside of marking it public is that class Y can modify the variable even if allowing that can break your code in class X.

Here's an example of how one class can break another's class' functionality this way:

 class X : MonoBehaviour
 {
      public GameObject a;

      void Update()
      {
          Debug.Log(a.GetComponent<Transform>().ToString()); // will not crash if "a" is not null.
      }
 }
 
 class Y : MonoBehaviour
 {
     void DoSomething(X x)
     {
          Debug.Log(a.GetComponent<Transform>().ToString()); // This is ok.

          x.a = null; //WHOOPS!! NULL REFERENCE EXCEPTION!
     }
 }

As you can see, leaving it exposed can be dangerous. So sometimes, you don't want a variable to be writable by other classes, but you still want to give other classes the ability to read the value.

This is where C# properties come in. They make it so that you can get a value or set a value, and both getting and setting can have their own access modifiers:

  class X
  {
       public int a
       {
            get;
            private set;
       }

       public X()
       {
           a = 20;
       }
  }

  class Y
  {
        void DoSomething(X x)
        {
            x.a = 3; // NOT ALLOWED
            Debug.Log(x.a) // ALLOWED, would print out 20.
        }
  }

Now that a is a property, it can be treated as if its a variable to other code, but its not really a variable. The private marker on set makes it so that only X can set a; this effectively makes it read-only to other code.

Properties are unique to C#, but the idea of making something read-only accessible to other classes is not. Many languages have ways for programmers to make their own "getters" and "setters", even though the getters and setters themselves are not part of the language. For instance, languages like C++ work around not having built-in getters and setters by making a functions that only return values or constant references of the real value:

 class X
 {
 public:
     int GetA() const
     {
          return mA;
     }

     X()
     {
          mA = 20;
     }

 private:
     int mA;
 };

 class Y
 {
      void DoSomething(const X& x)
      {
          x.mA = 3; // NOT ALLOWED
          std::cout << x.GetA(); //ALLOWED
      }
 };

Likewise, you can take the same approach and just write a "get" function instead of a "get" property, which as you should see now, essentially equate to the same thing:

 function GetCharacterController() : CharacterController
 {
   return m_Controller;
 }

 private var m_Controller : CharacterController;

[2]: http://ejohn.org/blog/javascript-getters-and-setters/

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 Eric5h5 · Mar 13, 2013 at 11:54 PM 0
Share

Properties are built-in to Unityscript. Remember that Unity does not use Javascript, even though it's officially called that for some reason.

avatar image
0

Answer by Garth-Smith · Mar 13, 2013 at 09:28 PM

That script should give a compile error. I'm going to assume the code is this:

 public CharacterController characterController { get { return m_Controller; } }
 private CharacterController m_Controller;

Accessing the characterController of this script would return m_Controller, however that gets set. This allows the script to control how m_Controller gets set, but still allow other classes to access it.

EDIT: See http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx

Comment
Add comment · Show 2 · 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 SirMalarkey · Mar 13, 2013 at 10:49 PM 0
Share

but how do i write that in java script?

avatar image Garth-Smith · Mar 13, 2013 at 11:05 PM 0
Share

I'm not really a JavaScript guy, but the first thing that pops into $$anonymous$$d is writing a function that returns m_Controller.

 function GetCharacterController() {
   return m_Controller;
 }
avatar image
1

Answer by Eric5h5 · Mar 13, 2013 at 11:54 PM

The equivalent of this in C#:

 CharacterController m_Controller;
 public CharacterController controller { get { return m_Controller; } }

Is this in Unityscript:

 private var m_Controller : CharacterController;
 function get controller() : CharacterController {return m_Controller;}

In both cases, the property is used exactly the same way in code (even though you use the function keyword in Unityscript, it behaves like a variable). e.g.,

 print (controller.name);
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

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Rays and tags help? 1 Answer

Fading To New Scene Problem? 1 Answer

I know C#, but what Unity methods should I learn? 2 Answers


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