O'Reilly logo

Unity 3 Game Development HOTSHOT by Jate Wittayabundit

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Properties with getters/setters

In C#, it is possible to define special functions that can be accessed as if they were variables. For instance, we could say foo.someVar = "testing";, and under the hood, there are get and set functions, which process the argument testing and store it internally. However, they could also do any other processing on it, for instance, capitalizing the first letter before storing it. So you're not just doing a variable assignment, you're calling a function that sets the variable, and it can do whatever the functions do.

C#:

public class MyClass {     
private int foo = 8;  //"backing store"      
public int Foo {         
    get {             
       return foo;         
    }         
    set {             
      foo = value;             
    }     
} 
}

However, in Unity JavaScript, we can also use get and set functions similar ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required