Tut78: Extra Exercise. [Basic]

Here’s a basic exercise designed to test your understanding of functions in C#.

Exercise:
In Petey’s BurgZergArcade tutorial episode #78: Day Night Cycle part 7, take the if statement and turn it into a function with an appropriate call in the Start() function.

void Start() {
if(GetComponent() != null)
givesLight = true;
}

Hint:

Write the function that accompanies this code:

void Start() {
CheckLight();
}

Solution:
public bool givesLight = false; //is the Light component attached to the object?

void Start() {
CheckLight();
}

//If the Light component is added to the Celestial body, check "true"
private void CheckLight() {
if(GetComponent() != null)
givesLight = true;
}

Explanation:
The function CheckLight is called in the Start() routine, and is then run from the exernal function. Sometimes this is desired because it will make the Start() routine more concise, and it allows easy modification as well as segmentation of your code. That means that it’s easier to update later.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: