How to start game when clicking a button

Hi guys, I am a beginner so bear with me. Basically I am trying to create a title screen in my game where there is an easy, medium, and hard mode. What I want to happen is when the player clicks one of these buttons the game starts and after the click the title screen goes away. Here is my DifficultyButton script. Any help or tips are appreciated because I feel like this is simple coding but I have no way to go about it, and ive tried looking up videos but none of them are really helping me. Thanks guys!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DifficultyButton : MonoBehaviour
{
    private Button button;
    // Start is called before the first frame update
    void Start()
    {
        button = GetComponent<Button>();
        button.onClick.AddListener(SetDifficulty);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void SetDifficulty()
    {
        Debug.Log(button.gameObject.name + " was clicked");
    }

}

Here is what I would suggest you.

  1. The key here is that in general UI elements are not unloaded while the game is in progress. If the UI overload is not too much, try hiding the UI once the user clicks the Difficulty level.
  • Assign a CanvasGroup to your UI elements.
  • When user selects Difficulty level, change CanvasGroup.alpha to zero.
  • Also start your game.
void SetDifficulty()
    {
        Debug.Log(button.gameObject.name + " was clicked");
        gameObject.GetComponent<CanvasGroup>().alpha = 0f;
        StartGame(); // You game start script
    }

Pro Tip: Use tools like DotWeen to smoothly change alpha value to zero for better UI. Also, when you pause or stop game, bring the UI back by setting alpha to 1.

All the best!

Hello mate,

Your difficulty system will change elements in your gameplay so in order for every object to know what difficulty you chose you will need a way for them to check that, there are many ways to do that and here are some of them:

  • You can make one unity Scene for each difficulty level and when the difficulty button is pressed you load the right Scene

  • You can save with the PlayerPref which difficulty level has been selected and then initialize your element after checking which difficulty it is

  • If you only have one Scene (which is recommended if you make mobile games for example) you can have an object with a script that will keep infos about the game like the difficulty

  • You can use Scriptable Object to keep track of your difficulty but it’s not recommended to use this approach when you begin and also not recommended to change the data of a scriptable asset (but when you get used to work with Scriptable Object it becomes a good way to share informations between scenes)

I would recommend you to start making the gameplay part of your game before focusing on having different difficulties.
I invite you to check a bit about every way i gave you and tell us which one suits your game / coding style so we can give you more informations on how to implement that.

I hope it helps

Hello again mate,

So let’s assume you have one scene (like I usually do on my mobile games),
I will give you a complete example so you can understand it well and adapt it to your code:

First you will need a script to manage the important datas about your current game session, for example you can have a System.cs with a State variable that will define your game state, System.cs can be a singleton so your units and objects can access it easily but I won’t do that here because you said you are a complete beginner, we will try the easy approach:

public enum States { // an enum to list every state we have
  Home,
  Playing,
  Win,
  Lose
}

public class System : MonoBehaviour {

   private States _state = States.None;
   public States state { // We can use a Getter / Setter in order to change our game state directly when the variable change, I invite you to look for it, if you don't understand I can make something simpler
    get { return this._state; }
    set {
      this._state = value;
      
      this.menuManager.HideAllMenus();
      this.menuManager.DisplayMenu(value);
    }

   public MenuManager menuManager;
   public int difficultyLevel;

   private void Start() {
      this.state = State.Home;
   }

}

Then you will have a MenuManager.cs to manage the menus:

public class MenuManager : MonoBehaviour {
   
   public MenuHome menuHome;
   public MenuGame menuGame;

   public void DisplayMenu(States state) {
       switch (value) { // A switch case on every possible values
          case States.Home:
            this.menuHome.Display();
            break;
          case States.Playing:
            this.menuGame.Display();
            break;
      }
   }

   public void HideAllMenus() {
      this.menuHome.Hide();
      this.menuGame.Hide();
   }

}

Each menu can inherit an Abstract class AMenu to be easily manipulated (we will keep it to the minimum to avoid confusion, I recommend you to learn about it, it will be very useful):

public abstract class AMenu : MonoBehaviour {

    public virtual void Display() {
        this.gameObject.SetActive(true);
    }

    public virtual void Hide() {
        this.gameObject.SetActive(false);
    }

}

Now you will have your MenuHome with the desired difficulty buttons:

public class MenuHome : AMenu { // The menu inherits from AMenu in order to have the Display and Hide methods

   public System system; // Can be changed with a singleton
   public Button[] bDifficulties;

   private void Awake() {
      for (int i = 0; i < this.bDifficulties.Length; i++) {
         int j = i;
         this.bDifficulties.onClick.AddListener(() => {
            this.SetDifficulty(j);
            this.system.state = State.Playing;
         }); // we populate the click events with the setup of the difficulty level;
      }
   }

   private void SetDifficulty(int difficultyLevel) {
      this.system.difficultyLevel = difficultyLevel;
   }
}

And a basic game menu:

public class MenuGame : AMenu {

   public System system

   private void OnEnabled() {
      print("Current difficulty level is " + this.system.difficultyLevel;
   }

}