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:
Read the rest of this entry »

Programming Error: Unreachable Code Detected

Explanation:
This is generally when there is a fragment of code that can never be accessed under any circumstance. It’s a very general category. It might happen if you create an infinite loop that has code after it – since it’s infinite, it will never end to execute the next line. It can also happen if there is an impossible if/else, switch or other control structure. “Impossible” does not allow for any circumstance where it can happen. It is logically impossible.

Example:
while(true) {
Something();
}
OtherThing();

Solution:
Read the rest of this entry »

Unity Error: ListenerException

ListenerException: Attempting to remove listener for type player health updated but Messenger doesn't know about this event type.

Explanation:
Messenger is trying to remove a listener, but it doesn’t recognize the one you passed it. This means that either it wasn’t told about the listener that you are sending it, or you have a typo.

Non-programming explanation:
You have a package at UPS with the tracking number #1000000 (your Listener) and you want to pick it up from UPS (Messenger). You go to the UPS store and tell them the tracking number is #1888888 by accident, but they cannot find your package, so they keep your real package in the back (your Listener is not removed).

Example:
Messenger.AddListener("status", OnChange);
Messenger.RemoveListener("statussss", OnChange);

Solution:
Read the rest of this entry »

Programming Error: Cannot implicitly convert type `float’ to `int’.

error CS0266: Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?)

Explanation:
This often happens when ints are used in an equation that would yield a non-int, such as a decimal (float). In the example, 100/3 is 33.33333, which is being stored in an int. An int cannot store decimals, you need a float or double for that.

Example:
int var = 100/3;

Solution:
Read the rest of this entry »