Game Development Reference
In-Depth Information
Figure 5-29. The message as a combination of strings and variables
Instead of “object,” it would be much better if it used the name of the object the script is on. If
you remember, the Object class has a variable called “name.” The GameObject class inherits from
Object, so you can get the name of the gameObject the script resides on.
8.
Change the line to the following:
Debug.Log("The " + gameObject.name + " has " + legs + " legs.");
9.
Save the script, and click Play.
The message is now customized for the object that each instance of the VariablesTest script is on
(Figure 5-30 ).
Figure 5-30. The message telling you the name of the object as well as its Legs value
The Conditional
Besides instructions, another extremely common element inside a function will be the conditional .
The conditional is the logic that often decides how your game will function under differing
circumstances.
Two of the messages are perfect, but the Sunflower, with only 1 leg, would be better if the message
said leg instead of legs . Obviously, for a printout in the console it's not an issue, but changing the
grammer according to the value of the variable will give you a good opportunity to get familiar with
one of the most useful bits of code in a game, the conditional , or if , statement. The conditional
 
Search WWH ::




Custom Search