Getters and Setters with Javascript and C#

In this Unity Scripting video tutorial I explain the use of ‘getter’ and ‘setter’ methods for the C# and Javascript flavors of Unityscript. They can be used to facilitate the reading and setting of another object’s properties in a transparent way, while still offering an extra layer of control in the object who’s property is being changed.

The C# example starts at 1:10; the Javascript one at 9:15.

You must be logged in to upload images. Register

Discussion

14 Responses to “Unity Scripting: Getters and Setters in Javascript and C#”
  1. Posts: 5

    Really interesting and a little difficult! I love your tutorials and I think the scripting’s world it’s huge!! We must to watch lots of tutorials if we want to understand all the things.

    #
    1
    Jan 18, 2012 at 11:06 am
  2. Posts: 3

    Hi Patrick, a really handy little tutorial.

    A question about C# in Unity:

    I would like to use the ‘Getter’ and ‘Setter’ to set the values of private variables in the inspector. But Unity doesn’t show those public properties. Only the public variables are visible in the inspector.

    Do you know how to solve this problem ? To see the public properties also in the inspector of Unity.

    #
    2
    Jan 18, 2012 at 1:25 pm
    • Posts: 109

      Hey Thoeppner,

      The reason it’s not showing the public ‘property’ is because it isn’t really a property. It’s just an accesso r method (or 2 actually) that looks like a property from the outside. In reality what you have is a private property (say, _hp) and 2 methods (gethp and sethp). Both of these types won’t show up in the inspector.

      -Patrick

      #
      2.1
      Jan 18, 2012 at 2:08 pm
      • Posts: 3

        Hm, that sounds plausible. Thanks !!!

        #
        Jan 18, 2012 at 11:17 pm
  3. Posts: 5

    Thank you so much! I love how you guys actually listen to the comments!

    #
    3
    Jan 18, 2012 at 9:45 pm
  4. wicked208
    Posts: 3

    great thanks, i love c#

    #
    4
    Jan 19, 2012 at 6:30 am
  5. Posts: 3

    Thank you very much for another wonderful tutorial. I just noticed that you must have really been burning the midnight oil being up at 3am! :D

    #
    5
    Jan 23, 2012 at 10:41 am
  6. Posts: 2

    Noob Question: Which language is more useful to learn for Unity, JavaScript or C#? Or a booth equal useful?

    #
    6
    Jan 28, 2012 at 4:11 pm
    • Posts: 109

      Hey Manderson,

      Aside from a few advanced functional limitations in Javascript, it’s mostly a matter of taste. JS is a bit looser than C# which has both pros and cons. Please note though that all Unity languages can be used in conjunction with each other without trouble, so you can easily complement one with the other when and if the need arises.

      #
      6.1
      Jan 28, 2012 at 6:24 pm
  7. HI I really liked this tutorial. A lot of stuff in Unity is self explanatory except scripting.
    Thanks
    On question though. How do I get that ScriptManager you have? I seems like a good thing to have.

    #
    7
    Feb 25, 2012 at 8:18 pm
    • Posts: 109

      Hi Thomas,

      Glad you liked it! The ScriptManager you saw is just an Empty GameObject that I renamed. =) It’s just a convention I like to use for storing scripts that serve a general or scene-wide/ overview purpose (i.e. Level scripts or helper functions).

      -Patrick

      #
      7.1
      Feb 25, 2012 at 8:44 pm
  8. Posts: 13

    can you explain again what a Setter and Getter does exactly
    I can see kind of what it does, but it still kinda confuses me

    #
    8
    Jun 5, 2012 at 7:11 pm
    • Posts: 109

      Hi Sarkhamy,

      Basically what they do is create functions (attributes) that you can access as if they were regular variables. So instead of having to type book.Title() you can type book.title.

      The reason they’re called getters/ setters is because they’re almost exclusively used to expose otherwise private variables. There are plenty of good reasons to keep members and variables private, but here’s a quick example.

      Let’s say it’s extremely important that all book titles always use capital letters on each new word. If we would make the title variable public, we could do things like ‘book.title = “CGCookie and the secret tutorial’, breaking our system of capitalization. Now if we were to use a setter instead, rather than setting the title to that string immediately, it could run the following code:

      //untested psuedo-code
      set{
      var processedTitle:String;
      var words:String[] = value.Split(‘ ‘); //split the title by spaces/ into words

      for(word:String in words){ //go over all the words
      word[0].ToUpper(); //capitalize the first letter of each word
      processedTitle += word + ” “; //add the newly capitalized word + a space to processedTitle
      }

      processedTitle.Trim(); //Removes the final trailing space
      _title = processedTitle; //Setting the private var _title to our capitalized string
      }

      Hope this cleared things up a bit!
      -Patrick

      #
      8.1
      Jun 5, 2012 at 7:55 pm

Leave a Comment

You must be logged in to post a comment.