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.


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.
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.
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
Hm, that sounds plausible. Thanks !!!
Thank you so much! I love how you guys actually listen to the comments!
great thanks, i love c#
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!
Ha, what can I say – I’m a night owl.
Noob Question: Which language is more useful to learn for Unity, JavaScript or C#? Or a booth equal useful?
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.
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.
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
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
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