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;
}
}