Game Development Reference
In-Depth Information
Step 27: Play the game. Move the mouse over the buttons and watch them
light up.
Why?
But there's a problem, no? The buttons light up but they stay lit. The
reason for this is that the script is looking for the frame where the
mouse moves over the button and on that frame it fires the command
guiTexture.color = Color (1,1,1);. Then it doesn't do anything to this
GameObject until the mouse again is over it. It means that the guiTexture
node never gets the instructions to go back to gray. But we can fix that.
Step 28: Reopen the OpenSceneButtonsScript script by double-clicking it
in the Project panel. Add the following text to the script:
function OnMouseExit (){
guiTexture.color = Color (.2,.2,.2);
}
Why?
Taking a look at this, you can see that the script just watches for when the
mouse exits the object, and then changes the color to RGB = 0.2 (which
happens to be the gray of the unhighlighted buttons). So we now have
a function that does one thing when the mouse is on the button, and
another when it leaves.
Step 29: Save, check the console for errors, fix any that appear, and test
the game. The buttons should highlight and darken appropriately.
Recognize What's Being Clicked
Alrighty. The buttons highlight and dehighlight, but they do nothing when
clicked. To make this happen we need to flush out the script still further.
Now, on the one hand we could create a custom script for each button that
said OnMouseDown activate the corresponding DataPanel. However, this
would have to be custom to each button since the Bio button would need to
activate the DataPanel_Biography object and deactivate DataPanel_Mission,
but it would be the opposite for the Mission button. Then, the Button_Begin
would need yet another collection of script to launch the next game level.
If Statements
Instead, let's look at a little more elegant approach and modify the existing
script to do all of this for us. To do this, we need Unity to check the name of
the object it is clicking. To do this we need to look at the idea of if statements .
The format for if statements goes like this (this isn't real code, just an illustration):
if(this is true){
do these things;
}
Search WWH ::




Custom Search