c# setter/getters

Hi I'm coming from a Java background. I cant find many resources on c# getters/setters in unity.

I have prefabs with the Block script attached to each. Each prefab is loaded into the MyBlocks[]

I just cant get/mod the values from the Block class in the class Player. So I'm really wondering how do c# setters and getters work in unity especially if I want to get/modify a private value from a class ?

 public class Block : MonoBehaviour 
    private bool b = false;
    public bool getB()
     get{return b;}
 public class Player : MonoBehaviour 
 public Block[] blockList;
 public int size = 6;
 public Block[] MyBlocks;
 void start()
    blockList = new Block[size];
    for(int i = 0; i < size; i++)
    int RandomNumber = Random.Range (0, MyBlocks.Length);
      MyBlocks[RandomNumber].transform.position ,
    blockList[i] = MyBlocks[RandomNumber];
    blockList[i].b = true; 
     Debug.Log("i: " + i + " " + blockList[i].b);

more ▼

asked Oct 15, 2013 at 05:46 AM

ConsciousCoder gravatar image

48 4 10 14

@conscious please TICK AN ANSWER as you are the OP. it's the round TICK symbol.

Oct 16, 2013 at 06:16 AM Fattie
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

C# has a system for getters and setters called Properties that Java doesn't (I probably don't need to tell you that Java =/= Javascript, but it seems to be a common mistake). These are a really neat way of controlling variables, because they abstract away from using explicit "getX()" and "setX()" methods, allowing them to be used similarly to a regular variable. In terms of actual execution, it is essentially running a method call each time you use it, but it does make the code cleaner. Let's take an example. In Java, you would write something like this:

 public int getX()
     return x;
 public void setX(int newX)
     if(newX < 0)
         x = 0;
         x = newX;

This is a simple getter and setter in Java (and, coincidentally, also valid C#), which constrains an integer to be positive, clamping negative inputs to 0. This allows you to use it in this manner:


This will increase the value by 4. This is how getters and setters work in Java, and they can be used this way in C#, too. However, C# has a neater way of doing it. This is the same getter and setter written in C# using Properties:

 public int X
     get { return x; }
         if(value < 0)
             x = 0;
             x = value;

This wraps the getter and setter into one item, allowing you to use the value in a more natural way. Instead of the (frankly rather ugly) code I used with the Java example to add 4 to the variable, instead we can do this:

 X += 4;

(Note that the X here is capitalised, because X is the property, which is distinct from x, which is a variable.) This essentially runs the same code as above, but it's much neater, and easier to understand what's going on. The value keyword is used to access the implicit argument of the set method, so you don't have to worry about explicitly defining it. You can also do things like this:

 public int Y
     get { return y; }
     private set { y = value; }

Which will allow only the parent class to write to this property, but anyone to read it (which is incredibly useful in a lot of situations). And:

 public int Z
     get { return z; }

Which creates a read-only property, so that no-one can write to it. This is most useful if you're returning the result of a calculation, rather than just a variable (note that the get block can contain more than just a return statement, as it is essentially just a method that returns the type that the property is defined to have, you can execute arbitrary code in there, as long as it obeys the rules of the language, of course).

Finally, there is also the implicit property:

 public int A
     get; set;

This implicitly creates a variable for the property to read and write to. In this instance, it would work in exactly the same way as a public variable, but you can use access modifiers etc., as above, to alter how you can access the property. For example:

 public int B
     get; private set;

I hope this has given you a bit of an insight into how properties work. It's worth remembering that, though these look like variables when you use them, they are not variables - they are an abstraction for getters and setters, and so when you access a property, you are calling a method, not accessing a variable. Performance-wise, this makes them less efficient than variables, but more efficient than separate getter and setter methods, as they are easier for the compiler to optimise.

more ▼

answered Oct 15, 2013 at 06:30 AM

Hoeloe gravatar image

6.1k 50 58 85

Just a side note on coding convention regarding properties, @Hoeloe you mention that the property name is capitalized. The way you say it sounds like it is always as such. It is true almost everywhere in the .NET world but Unity.

The properties from .NET keep the convention


while those from Unity do not:


So it can get confusing sometimes thinking you are facing a variable and it happens to be a property...

Still the whole thing you say is right.

Oct 15, 2013 at 09:09 AM fafase

Thanks! but ... What the heck is XAML man ??

(Regarding your second paragraph: for sure (astonishingly) I utterly understand that; my question is when you DON'T use any qualifier whatsoever, then, in fact, is it utterly pointless and the code should be changed to a variable and the programmer in question sacked?)

Oct 15, 2013 at 09:10 AM Fattie

Hmm - is this something to do with "Visual Editor" (which, I believe, is some sort of extremely complex text editor, with some inherent language understanding, autocompletion, function funding etc, for people who don't just use TextEdit or BBEdit, right?)

Oct 15, 2013 at 09:11 AM Fattie

Magnificent - you've answered the question, thanks. In short, the reason in some projects I see a proliferation of these, is that, it is used by the typical code automation on Windows machines. Awesome. In fact (setting that aside) there is zero reason for it, if it's just a straight unqualified usage. Thanks!!!!!

Oct 15, 2013 at 09:41 AM Fattie

"I wish Unity supported properties" FTR I don't fully understand you there.

Oct 15, 2013 at 09:41 AM Fattie
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Oct 15, 2013 at 05:46 AM

Seen: 11812 times

Last Updated: Sep 30 at 07:06 AM