Functions and Methods Flashcards
Difference between functions and methods
Method: In programming, a method is a code block containing a series of statements. It is a reusable block of code that performs a specific task. In Unity, methods are defined within classes and can be called to execute specific actions or calculations.
Function: A function is a broader term that encompasses methods. It refers to a block of code designed to perform a specific task, which may or may not return a value. Functions can include methods as well as other constructs like properties, events, and more.
In Unity C#, when people refer to functions, they often mean methods specifically because methods are the primary way to define executable behavior within classes. Therefore, while functions and methods are technically different in a wider programming context, they are functionally equivalent in Unity C# discussions, where methods are the primary means of defining behavior within scripts attached to game objects.
Start()
Description:
Start() is called on the frame when a script is enabled just before any of the Update methods are called the first time.
It is used for initialization.
When to Use:
Use Start() to set up variables, initialize game objects, or perform any setup required for your game or script.
Update()
Description:
Update() is called once per frame.
It is the primary function for implementing any behavior that needs to be checked or executed frequently.
When to Use:
Use Update() for tasks such as checking for input, moving characters, or performing time-dependent actions.
FixedUpdate()
Description:
FixedUpdate() is called at a fixed time interval.
It is used for physics-related updates.
When to Use:
Use FixedUpdate() for physics calculations and adjustments, such as applying forces to a Rigidbody.
LateUpdate()
Description:
LateUpdate() is called once per frame, after all Update() functions have been called.
It is used for actions that need to be executed after all other updates.
When to Use:
Use LateUpdate() for following a camera, final adjustments to objects that depend on other scripts’ updates.
OnCollisionEnter()
Description:
OnCollisionEnter() is called when a collision occurs with another collider.
It provides information about the collision through the Collision parameter.
When to Use:
Use OnCollisionEnter() to detect and respond to physical interactions, such as when a character lands on a platform.
OnTriggerEnter()
Description:
OnTriggerEnter() is called when another collider enters a trigger collider attached to the object.
It uses a Collider parameter to identify the entering object.
When to Use:
Use OnTriggerEnter() for non-physical interactions like entering a zone, triggering an event, or collecting an item.
Awake()
Description:
Awake() is called when the script instance is being loaded.
It is used for initialization that happens before Start()
When to Use:
Use Awake() to set up references between scripts and initialize variables that need to be set before the game starts.
OnDestroy()
Description:
OnDestroy() is called when the MonoBehaviour will be destroyed.
It is used for cleanup tasks.
When to Use:
Use OnDestroy() to release resources, save game data, or clean up anything that needs to be reset when the object is destroyed.
OnEnable()
Description:
OnEnable() is called when the object becomes enabled and active.
It can be used to perform actions when an object is activated.
When to Use:
Use OnEnable() for tasks such as subscribing to events, starting animations, or resetting object states.
OnDisable()
Description:
OnDisable() is called when the object becomes disabled.
It is used to perform actions when an object is deactivated.
When to Use:
Use OnDisable() to unsubscribe from events, stop animations, or save states.