Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Tutorial on buttons in unity

A topic by piyushchourasia05 created Apr 15, 2021 Views: 653
Viewing posts 1 to 1

Buttons in unity

 

 

To insert a button, right click in the Scene Hierarchy and go to Create → UI → Button. If you do not have an existing Canvas and an EventSystem, Unity will automatically create one for you, and place the button inside the Canvas as well.

A button can serve some basic as well as advanced purpose as well like switching between scenes, playing audio , video etc.

Using a button a transition can be made between the scenes or a message can be displayed.

How to add a button   ??

  • Create a new project using unity.

  • Go to ui and then buttons.

Your button is created. It is created in a canvas and will only b1e visible only when in canvas.

 

Making a program using buttons to count how many times the button is clicked.

 

  • Create a new script using the create option in asset folder.


2)after creating the script create an empty gameobject that will serve as a container for the script.


3)the code for the script will be-

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

 

public class counting : MonoBehaviour

{

   public int n;

    public void OnButtonClick()

    {

        n++;

        Debug.Log("Button clicked " + n + " times.");

    }

}

 The logic behind the code is that each time the button is clicked the function is called which increases the value of n  and that value is printed in the console window.

  4) now click the + icon on OnClick() in inspector window of the button then go to no function and select the function that you declared in the script.


5) Now play the program.

Here is the output of the program

How to play audio using a button.

  Create a new script using the create option in asset folder


2)after creating the script create an empty gameobject that will serve as a container for the script.


3)the code for the script will be-

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.SceneManagement;

 

public class NewBehaviourScript : MonoBehaviour

{

    public AudioSource Nature;

    public void playClip()

    {

        Nature = GetComponent<AudioSource>();

        Nature.Play();

    }

}

The logic behind the code is that when the button is clicked the function is called it gets the audio component which you added in the game component and then plays it. now click the + icon on OnClick() in inspector window of the button then go to no function and select the play() 



4)If you don’t want the sound to be played when scene is called or make it loop. You have to go into the game object and uncheck the loop and play on wake option.

  • Now when you play the scene and click the button the sound would start to play.

 

 

How to change scene using buttons.

  Create a new script and a new scene using the create option in asset folder

2)after creating the script create an empty gameobject that will serve as a container for the script.

3)the code for the script will be-

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.SceneManagement;

 

public class Scene : MonoBehaviour

{

    // Start is called before the first frame update

    public void NextScene()

    {

        SceneManager.LoadScene(1);

    }

}

The logic for the code would be whenever the button is clicked the function is called which triggers the LoadScene command which in turns calls the scene mentioned within parensynthesis and the scene is changed.

Make sure all the scene is called in build setting otherwise it will show an error.

 

  • Now play the scene and after you click the button the scene will change

 

 

Thank you

Made by

Piyush Chourasia 20BCG10054   

Gunnet Singh 20BCG10065

 

 GitHub link

https://github.com/piyushchourasia05/tutorial-on-button.git