I try to get the amount of number of objects destroyed displayed when the game is over, but for some reason the value don`t change.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlueWall : MonoBehaviour
{
public static BlueWall Instance { get; private set; }
[SerializeField] private GameObject blueWall;
[SerializeField] private GameObject blueProjectile;
private int blueWallDestroyAmount;
private void Start()
{
Instance = this;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("BlueP"))
{
blueWallDestroyAmount++;
Destroy(gameObject);
Destroy(other.gameObject);
}
else
{
Destroy(other.gameObject);
}
}
public int GetBlueWallDestroyAmount()
{
return blueWallDestroyAmount;
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IGameManager : MonoBehaviour
{
public static IGameManager Instance { get; private set; }
public event EventHandler OnStateChanged;
private enum State
{
WaintingToStart,
CountdownToStart,
GamePlaying,
GameOver,
}
private State state;
private float waitingToStartTimer = 1f;
private float countdownToStartTimer = 3f;
private void Awake()
{
Instance = this;
state = State.WaintingToStart;
}
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;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class GameOverUI : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI blueWallDestroyText;
[SerializeField] private TextMeshProUGUI redWallDestroyText;
[SerializeField] private TextMeshProUGUI greenWallDestroyText;
[SerializeField] private TextMeshProUGUI yellowWallDestroyText;
private void Start()
{
IGameManager.Instance.OnStateChanged += IGameManager_OnStateChanged;
// Hide();
}
private void IGameManager_OnStateChanged(object sender, System.EventArgs e)
{
if (IGameManager.Instance.IsGameOver())
{
Show();
blueWallDestroyText.text = BlueWall.Instance.GetBlueWallDestroyAmount().ToString();
yellowWallDestroyText.text = YellowWall.Instance.GetYellowWallDestroyAmount().ToString();
}
// else
// {
// Hide();
// }
}
private void Show()
{
gameObject.SetActive(true);
}
// private void Hide()
// {
// gameObject.SetActive(false);
// }
}