Game Development Reference
In-Depth Information
There are two diferent ways of casing in C#. For the irst line in the
preceding code, if the object can't be instaniated, it will throw an excepion.
You would need to use a try / catch to properly handle it. The second line,
if it fails, will set foo to null , and not throw an excepion. Then you would
just need to test, if the returned object was null .
Properties with getters/setters
In C#, it is possible to deine special funcions 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 funcions, which process the argument tesing and store it internally. However,
they could also do any other processing on it, for instance, capitalizing the irst leter before
storing it. So you're not just doing a variable assignment, you're calling a funcion that sets
the variable, and it can do whatever the funcions 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 funcions similar to the
C# version, but we need to write the class body whenever you want to use the get or
set funcion.
JavaScript:
public class MyClass {
private var foo = 8; //"backing store"
function get Foo () : int {
return foo;
}
function set Foo (value) {
foo = value;
}
}
 
Search WWH ::




Custom Search