Skip to content

Latest commit

 

History

History
60 lines (37 loc) · 6.91 KB

Unity-Scripting-01.md

File metadata and controls

60 lines (37 loc) · 6.91 KB

Reference Sheet - Unity C# Scripting - 01

MonoBehaviour

  • MonoBehaviour is the base class from which every Unity script derives.
  • When you use C#, you must explicitly derive from MonoBehaviour.

Monobehavior Methods

Method Description
Awake() Awake is called just once, when the script instance is being loaded.
Start() Start is called just once, on the frame when a script is enabled, just before any of the Update methods are called the first time. This makes it a better place to store references to the game object's components needed in the script.
Update() The Monobehavior method that runs every frame.
FixedUpdate() FixedUpdate occurs at a measured time step with the physics system and typically does not coincide with MonoBehaviour.Update. At slow framerates it can occur 2 times per frame, and normal frame rates it will run once per frame, and fast frame rates every other frame. You should use it instead of Update() for all physics calculations.
OnTriggerEnter2D(Collider2D) Sent when another object enters a trigger collider attached to this object (2D physics only). Further information about the other collider is reported in the Collider2D parameter passed during the call.

data types
TutorialsTeacher C# Data Types

Terminology

Term Definition
Variable (value type) There are two kinds of types in C#: value types and reference types. Variables of value types directly contain their data. With value types, the variables each have their own copy of the data, and it is not possible for operations on one to affect the other (except in the case of ref and out parameter variables). C#’s value types are further divided into simple types (e.g. float,int,bool), enum types, struct types, and nullable value types.
Variable (reference type) Reference types store references to their data, the latter being known as objects. With reference types, it is possible for two variables to reference the same object and thus possible for operations on one variable to affect the object referenced by the other variable. C#’s reference types are further divided into class types (e.g. object, string, class), interface types, array types, and delegate types.
Method A method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method and specifying any required method arguments.
Inheritance Allows you to define a child class that reuses (inherits), extends, or modifies the behavior of a parent class. The class whose members are inherited is called the base class. The class that inherits the members of the base class is called the derived class. Microsoft.
Namespace Help to prevent naming conflicts by organizing our class names into collections referred to using the chosen prefix. An example of this simplified access: instead of typing UnityEngine.Debug.Log() we can type Debug.Log() by writing using UnityEngine.
Scope A variable can be either of global or local scope. A global variable is a variable declared in the main body of the source code, outside all functions, while a local variable is one declared within the body of a function or a block. Wikipedia.
Scriptable objects Scriptable objects are reusable data containers defined via C# script and saved as Assets. They reduce memory usage by storing references to a single instance of a data set. Because they inherit from ScriptableObject class they cannot be attached to GameObjects as they do not inherit from MonoBehaviour.
Operators Operator symbols represent the arithmetic, assignment, relational, and logical functionality that types can perform. Arithmetic operators (+, -, *, /, %) represent basic math functions, while assignment operators (=, +=, -=, ...) perform math and assignment functions together on a given value. Relational (==, >, <, >=, <=, !=) and logical (&&, ||, !) operators evaluate conditions between multiple values, such as greater than, less than, and equal to. Read more about C# operators.

Tips

  • In VisualStudio Code on Mac, shift + option + F will auto format.
  • In C# the filename (e.g. PlayerController.cs) and the Class name (e.g. PlayerController) must match.
  • If you see an error like The type or namespace name <Type> could not be found. Are you missing a <namespace> using directive?, you may have forgotten to import the correct namespace at the top of your script.

Sources

  • Unity Manual and Unity Scripting Reference
  • Chapter 3 in Ferro, Lauren S., and Francesco Sapio. Unity 2017 Game Development. Packt, 2018.
  • Chapter 1 in Hocking, Joseph. Unity in Action: Multiplatform Game Development in C # (2nd Edition). Manning, 2018.
  • Chapter 1 in DaGraca & Lukosek. Learning C# 7 By Developing Games with Unity 2017 (3rd Edition). Packt, 2017.
  • Chapter 5 in Halpern, Jared. Developing 2D Games with Unity. APress, 2019.
  • Chapters 1–4 in Ferrone. Learning C# by Developing Games with Unity 2020 (5th Edition). Packt, 2020.