Adding pause button in the game using Unity

Advertisemen

In this short tutorial, we will learn how to create a pause button for your game to allow users to pause a game when they need to do something or any other reasons while playing a game.

I will try to demonstrate in as simplest as possible so everyone can understand.

Ok, first thing we are going to add in the scene is a sphere [ GameObject > 3D Object > Sphere ] and also add rigidbody to it [ Component > Physics > Rigidbody ]. We are doing this because we want to see the ball stop from falling when we hit the pause button.

Second thing we are going to add in the scene is button [ GameObject > UI > Button ]. Now, create one script, name it whatever you want and attached to the button and open the script then create public PauseGame function. Your script should look like this.

using UnityEngine;
using System.Collections;

public class pause : MonoBehaviour 
{

    public void PauseGame()
    {
        Time.timeScale = 0;
    }
}

Once you are done with the script, go back to Unity and select the button then in inspector panel find the button script and create new activity in OnClick by hitting the plus button then drag button itself into it and select the function we just created.


That's all you need to do. Now hit play and hit the pause button, it should stop the sphere from falling.


If you want to create a play button, just copy, paste and rename the function and give timescale a value 1.

Thank you very much reading this article

Subscribe to my YouTube channel for video tutorials :
Aarlangdi Production


Advertisemen

Disclaimer: Gambar, artikel ataupun video yang ada di web ini terkadang berasal dari berbagai sumber media lain. Hak Cipta sepenuhnya dipegang oleh sumber tersebut. Jika ada masalah terkait hal ini, Anda dapat menghubungi kami disini.

Tidak ada komentar:

Posting Komentar

© Copyright 2017 Tutorial Unity 3D