Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Keeping track of number of objects destroyed after the game is over

Discussion in 'Scripting' started by Danielddw, Oct 17, 2023.

  1. Danielddw

    Danielddw

    Joined:
    Dec 22, 2022
    Posts:
    13
    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.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BlueWall : MonoBehaviour
    6. {
    7.  
    8.     public static BlueWall Instance { get; private set; }
    9.  
    10.     [SerializeField] private GameObject blueWall;
    11.     [SerializeField] private GameObject blueProjectile;
    12.  
    13.  
    14.     private int blueWallDestroyAmount;
    15.  
    16.     private void Start()
    17.     {
    18.         Instance = this;
    19.     }
    20.     void OnTriggerEnter2D(Collider2D other)
    21.     {
    22.  
    23.         if (other.CompareTag("BlueP"))
    24.         {
    25.             blueWallDestroyAmount++;
    26.             Destroy(gameObject);
    27.             Destroy(other.gameObject);
    28.         }
    29.         else
    30.         {
    31.          
    32.             Destroy(other.gameObject);
    33.         }
    34.  
    35.     }
    36.     public int GetBlueWallDestroyAmount()
    37.     {
    38.         return blueWallDestroyAmount;
    39.     }
    40. }
    41.  
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class IGameManager : MonoBehaviour
    7. {
    8.     public static IGameManager Instance { get; private set; }
    9.  
    10.     public event EventHandler OnStateChanged;
    11.    private enum State
    12.     {
    13.         WaintingToStart,
    14.         CountdownToStart,
    15.         GamePlaying,
    16.         GameOver,
    17.     }
    18.  
    19.     private State state;
    20.     private float waitingToStartTimer = 1f;
    21.     private float countdownToStartTimer = 3f;
    22.  
    23.     private void Awake()
    24.     {
    25.         Instance = this;
    26.         state = State.WaintingToStart;
    27.     }
    28.  
    29.     private void Update()
    30.     {
    31.         switch (state)
    32.         {
    33.             case State.WaintingToStart:
    34.                 waitingToStartTimer -= Time.deltaTime;
    35.                 if(waitingToStartTimer < 0f)
    36.                 {
    37.                     state = State.CountdownToStart;
    38.                     OnStateChanged?.Invoke(this, EventArgs.Empty);
    39.                 }
    40.                 break;
    41.  
    42.             case State.CountdownToStart:
    43.                 countdownToStartTimer -= Time.deltaTime;
    44.                 if(countdownToStartTimer < 0f)
    45.                 {
    46.                     state = State.GamePlaying;
    47.                     OnStateChanged?.Invoke(this, EventArgs.Empty);
    48.                 }
    49.  
    50.                 break;
    51.             case State.GamePlaying:
    52.                 state = State.GamePlaying;
    53.                 if(Player.Instance.gameOver == true)
    54.                 {
    55.                     state = State.GameOver;
    56.                 }
    57.  
    58.                 OnStateChanged?.Invoke(this, EventArgs.Empty);
    59.                 break;
    60.             case State.GameOver:
    61.              
    62.              
    63.                     state = State.GameOver;
    64.              
    65.                 OnStateChanged?.Invoke(this, EventArgs.Empty);
    66.              
    67.                 break;
    68.              
    69.         }
    70.         Debug.Log(state);
    71.     }
    72.     public bool IsGamePlaying()
    73.     {
    74.         return state == State.GamePlaying;
    75.     }
    76.  
    77.     public bool IsCountdownToStartActive()
    78.     {
    79.         return state == State.CountdownToStart;
    80.     }
    81.  
    82.     public float GetCountdownToStartTimer()
    83.     {
    84.         return  countdownToStartTimer;
    85.     }
    86.  
    87.     public bool IsGameOver()
    88.     {
    89.         return state == State.GameOver;
    90.     }
    91. }
    92.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class GameOverUI : MonoBehaviour
    7.  
    8.  
    9. {
    10.     [SerializeField] private TextMeshProUGUI blueWallDestroyText;
    11.     [SerializeField] private TextMeshProUGUI redWallDestroyText;
    12.     [SerializeField] private TextMeshProUGUI greenWallDestroyText;
    13.     [SerializeField] private TextMeshProUGUI yellowWallDestroyText;
    14.     private void Start()
    15.     {
    16.         IGameManager.Instance.OnStateChanged += IGameManager_OnStateChanged;
    17.  
    18.        // Hide();
    19.     }
    20.  
    21.  
    22.     private void IGameManager_OnStateChanged(object sender, System.EventArgs e)
    23.     {
    24.         if (IGameManager.Instance.IsGameOver())
    25.         {
    26.             Show();
    27.  
    28.             blueWallDestroyText.text = BlueWall.Instance.GetBlueWallDestroyAmount().ToString();
    29.             yellowWallDestroyText.text = YellowWall.Instance.GetYellowWallDestroyAmount().ToString();
    30.         }
    31.       //  else
    32.       //  {
    33.         //    Hide();
    34.        // }
    35.     }
    36.  
    37.  
    38.     private void Show()
    39.     {
    40.         gameObject.SetActive(true);
    41.     }
    42.   //  private void Hide()
    43.    // {
    44.       //  gameObject.SetActive(false);
    45.    // }
    46. }
    47.  
    48.  
     
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,709
    Every time you increment blueWallDestroyAmount you also destroy the wall itself (that holds the value) so the value is of course lost.

    Code (CSharp):
    1.  
    2. if (other.CompareTag("BlueP"))
    3. {
    4.             blueWallDestroyAmount++;
    5.             Destroy(gameObject); //<-- you're destroying the wall gameObject here.
    6.             Destroy(other.gameObject);
    7. }
    In fact accessing BlueWall.Instance.GetBlueWallDestroyAmount() after the value has been incremented will probably result in a NullRefException, since the Instance itself has been destroyed.
     
  3. Danielddw

    Danielddw

    Joined:
    Dec 22, 2022
    Posts:
    13
    I know that Im destroying that game object there...but what should I do to have the number of destroyed items displayed when the game is over? I dont get a NullRefExeption but the number of destroyed objects doesnt change when I destroy them, but when the player is destroyed, the value change to 0.
     
  4. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,709
    Simply don't store the number of destroyed items in the object you're destroying (for obvious reasons: once the object doesn't exist anymore neither does the count), store it elsewhere.

    Btw, if you want to have more than one BlueWall in your game -I strongly suspect you do- you shouldn't use a singleton pattern like you seem to be doing.

    Singleton assumes there will only ever be a single instance of BlueWall in the game. Worse still, you singleton is bugged since it doesn't enforce a single instance and BlueWall.Instance will return whichever BlueWall instance had its Start() method called last, so in practice you're getting one instance completely at random.
     
    Last edited: Oct 20, 2023