Join Kelley Hecker for an in-depth discussion in this video Defining classes and variables, part of Scripting Unity with C#.
- Each script you create will be its own class,…with the class name matching the file name.…To create a new C# script,…right click in the project window and select…create C# script.…Since the script's file name is the same as the class name,…all your script files must have unique names.…Double click the script to open it in MonoDevelop.…You can see it already has a basic class implementation…along with start and update functions,…which we'll talk more about later.…
Variables are defined by specifying the access modifier…public, private, or protected,…the type of the object, and the variable name.…If you don't specify the access modifier,…it will default to private.…Since C# is statically typed,…you have to specify the type of the object here.…I like to place class member variables…at the top of the class, before any of the functions.…You can see how functions are defined by looking…at the start and update functions.…
Just like defining a variable, you can specify…an access modifier,…or leave it blank to default to private.…
- Defining classes and variables
- Attaching scripts to objects
- Understanding MonoBehaviour class
- Using the Unity Scripting API
- Importing assets
- Using a singleton pattern to manage game state
- Creating an event
Skill Level Intermediate
Q: Why am I getting a script compile error when trying to load the exercise files?
A: This course was recorded in Unity 4.6, and Unity has since released Unity 5. There are two items to address for Unity 5 users.
Q: Why can't I see the Render Settings option in the Edit menu?
A: The Render Settings options have been moved in Unity 5. You can access them by going to the Window menu and choosing Lighting. In the window that loads, Render Settings can be found under the Scene tab.