I want to set the difficulty of the game from buttons and I cant figure out how to do it. I tried different methods and I searched how to do it but I didnt find something to help me so far.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class DifficultyButton : MonoBehaviour
{
public static DifficultyButton Instance { get; private set; }
public int difficulty;
private Button button;
public void Awake()
{
Instance = this;
}
private void Start()
{
button = GetComponent<Button>();
button.onClick.AddListener(SetDifficulty);
button.onClick.AddListener(() =>
SceneManager.LoadScene(5));
}
public void SetDifficulty()
{
Debug.Log(gameObject.name + "Was clicked");
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class IGameManager : MonoBehaviour
{
public static IGameManager Instance { get; private set; }
public event EventHandler OnStateChanged;
public event EventHandler OnGamePaused;
public event EventHandler OnGameUnpaused;
private enum State
{
WaintingToStart,
CountdownToStart,
GamePlaying,
GameOver,
}
private State state;
private float waitingToStartTimer = 1.5f;
private float countdownToStartTimer = 3f;
private bool isGamePaused = false;
public void Awake()
{
Instance = this;
state = State.WaintingToStart;
}
private void Start()
{
GameInput.Instance.OnPauseAction += GameInput_OnPauseAction;
}
private void GameInput_OnPauseAction(object sender, EventArgs e)
{
TogglePauseGame();
}
private void Update()
{
switch (state)
{
case State.WaintingToStart:
waitingToStartTimer -= Time.deltaTime;
if(waitingToStartTimer < 0f)
{
state = State.CountdownToStart;
OnStateChanged?.Invoke(this, EventArgs.Empty);
}
break;
case State.CountdownToStart:
countdownToStartTimer -= Time.deltaTime;
if(countdownToStartTimer < 0f)
{
state = State.GamePlaying;
OnStateChanged?.Invoke(this, EventArgs.Empty);
}
break;
case State.GamePlaying:
state = State.GamePlaying;
if(Player.Instance.gameOver == true)
{
state = State.GameOver;
}
OnStateChanged?.Invoke(this, EventArgs.Empty);
break;
case State.GameOver:
state = State.GameOver;
OnStateChanged?.Invoke(this, EventArgs.Empty);
break;
}
Debug.Log(state);
}
public bool IsGamePlaying()
{
return state == State.GamePlaying;
}
public bool IsCountdownToStartActive()
{
return state == State.CountdownToStart;
}
public float GetCountdownToStartTimer()
{
return countdownToStartTimer;
}
public bool IsGameOver()
{
return state == State.GameOver;
}
public void TogglePauseGame()
{
isGamePaused = !isGamePaused;
if(isGamePaused)
{
Time.timeScale = 0f;
OnGamePaused?.Invoke(this, EventArgs.Empty);
}
else
{
Time.timeScale = 1f;
OnGameUnpaused?.Invoke(this, EventArgs.Empty);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnManager : MonoBehaviour
{
public static SpawnManager Instance { get; private set; }
public GameObject[] wallPrefab;
private float startDelay = 4f;
public float spawnInterval = 2f;
public void Awake()
{
Instance = this;
}
public void Start()
{
InvokeRepeating("SpawnRandomWall", startDelay, spawnInterval);
}
public void SpawnRandomWall()
{
int wallIndex = Random.Range(0, wallPrefab.Length);
Vector2 spawnPos = new Vector2(10, 0);
Instantiate(wallPrefab[wallIndex], spawnPos, wallPrefab[wallIndex].transform.rotation);
}
}