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

Generics

The C# syntax supports generics that allows you to use classes and methods, which do not specifically declare a type. Instead, the type is passed as a parameter when calling the method or instantiating the class at runtime.

.Net comes with some useful generic classes, such as the List and Dictionary, and Unity's own API has some generic functions, which remove the need for some of the verbose casting that would otherwise be necessary in C#.

JavaScript:

//Automatically cast the correct type
var someScript : MyScript = GetComponent(MyScript);

//or using the Generic version in Javascript
var someScript : MyScript = GetComponent.<MyScript>();

C#:

//with out Generic var someScript : MyScript = (MyScript)GetComponent(typeof(MyScript)); //or using ...

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