Functions and Methods Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Difference between functions and methods

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Start()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Update()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

FixedUpdate()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

LateUpdate()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

OnCollisionEnter()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

OnTriggerEnter()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Awake()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

OnDestroy()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

OnEnable()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

OnDisable()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly