O'Reilly logo

Unity 3D Game Development by Example by Ryan Henson Creighton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - add the Play Again button

The very last thing we need to do is to add a button to the end of the game so that the player can play again. Let's revisit our good friend GUI from the last few chapters.

Add the OnGUI function to the HeartBounce script:

function OnGUI(){
if(hasLost){
var buttonW:int = 100; // button width
var buttonH:int = 50; // button height
var halfScreenW:float = Screen.width/2; // half of the Screen width
var halfButtonW:float = buttonW/2; // Half of the button width
if(GUI.Button(Rect(halfScreenW-halfButtonW, Screen.height*.8, buttonW, buttonH), "Play Again"))
{
numHits = 0;
hasLost = false;
transform.position = Vector3(0.5,2,-0.05);
rigidbody.velocity = Vector3(0,0,0);
}
}
}

What just happened?

For GUI pros ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required