2.4 Functions and Methods in C# for Unity
2.4 Functions and Methods in C# for Unity
Learning Objectives
2
Content
• Methods in Unity
• Coroutine
• Generic Functions
• Event Functions
• Script Serialization
3
Methods in Unity
Some Unity-specific methods commonly used in C# scripts:
1. Start() and Awake():
• Start(): Called once per frame when the script is enabled, after Awake(). It's typically
used for initializing variables or setting up references.
• Awake(): Called when the script instance is being loaded. It's used for initializing
variables or setting up references before Start().
2. Update():
• Called once per frame. It's often used for game logic that needs to be updated
continuously, like character movement or input processing.
• These are known as generic functions. The significance they have for scripting is that
you get to specify the types of parameters and/or the return type when you call the
function.
• Detect mouse events over a GameObject for actions like targeting or displaying character
information. OnMouseXXX event functions (e.g., OnMouseOver, OnMouseDown) enable
scripts to react to mouse actions. For instance, if the mouse button is pressed over an
object, the object's script's OnMouseDown function is called if it exists.