Search Unity

Help: Wave Number Updating

Discussion in 'Scripting' started by Squatch-A-Toon, Apr 18, 2021.

  1. Squatch-A-Toon

    Squatch-A-Toon

    Joined:
    Jun 27, 2020
    Posts:
    3
    I've been trying to get the waive number Text to update for a while now and while I can get the score to update no problem I just can't seem to get the Wave Number text Updating. I'm trying to get the wave number text to update when the number of enemies reaches 0, and spawns one more total enemy. ex: game starts with one enemy on wave one, once that enemy is defeated your on wave 2 and 2 enemies are spawned etc... My Number of enemies increases but not my wave number does not it just stays at 1. Can someone help me on this? I've tried inheritance and Delegates to try and solve this problem. I know its probably gonna be a simple fix but I just can't think of it or find the right solution.

    SpawnManager

    Code (CSharp):
    1. public class SpawnManager : GameManager
    2. {
    3.     public GameObject enemie;
    4.     private float _spawnRange = 20;
    5.     public int wave = 1;
    6.     public int enemyCount;
    7.     private GameManager _gameManager;
    8.     private void Start()
    9.     {
    10.         _gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
    11.         SpawnEnemyWave(wave);
    12.     }
    13.     private void Update()
    14.     {
    15.         enemyCount = FindObjectsOfType<Enemy>().Length;
    16.         if(enemyCount == 0 )
    17.         {
    18.             wave++;
    19.             SpawnEnemyWave(wave);
    20.          
    21.         }
    22.     }
    23.     private Vector3 GenerateSpawnPosition()
    24.     {
    25.         float spawnPosX = Random.Range(-_spawnRange, _spawnRange);
    26.         float spawnPosZ = Random.Range(-_spawnRange, _spawnRange);
    27.         Vector3 randomPos = new Vector3(spawnPosX, 0, spawnPosZ);
    28.         return randomPos;
    29.     }
    30.     void SpawnEnemyWave(int enemiesToSpawn)
    31.     {
    32.         for(int i = 0; i < enemiesToSpawn; i++)
    33.         {
    34.             Instantiate(enemie, GenerateSpawnPosition(), enemie.transform.rotation);
    35.         }
    36.     }
    Game Manager Class

    Code (CSharp):
    1. public class GameManager : MonoBehaviour
    2. {
    3.     public TextMeshProUGUI scoreText;
    4.     private int _score;
    5.     public TextMeshProUGUI waveNumber;
    6.     private int _currentWave;
    7.     private SpawnManager _spawnManager;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         _spawnManager = GameObject.Find("SpawnManager").GetComponent<SpawnManager>();
    12.         UpdateScore(0);
    13.         _currentWave = _spawnManager.wave;
    14.         UpdateWave(_currentWave);
    15.     }
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.      
    20.     }
    21.     public void UpdateScore(int ScoreToAdd)
    22.     {
    23.         _score += ScoreToAdd;
    24.         scoreText.text = "Score: " + _score;
    25.     }
    26.     public void UpdateWave(int Wave)
    27.     {
    28.         waveNumber.text = "Wave: " + _currentWave;
    29.     }
     
    Last edited: Apr 18, 2021
  2. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    348
    Are you getting confused between two different instances of your GameManager class?

    If I had to guess by those finds in your Start functions, you're putting a GameManager on a game object, then a SpawnManager on a game object too. If that's the case, now you've got two GameManagers. One is the one on a game object, and one is the parent of your SpawnManager class. It'd be quite easy to accidentally work with the wrong one.

    If SpawnManager inherits from GameManager, you don't need to put both on a game object and find them. With inheritance, the child is a parent. In this case, the SpawnManager is a GameManager. As a result, if you put a SpawnManager on a game object it already carries its parent GameManager with it. You can just say 'base.WhateverYouWantToAccessInTheParent' in your SpawnManager. No finding needed, they're the same thing.

    Simplify things a bit first.

    I'd start by getting rid of the inheritance and just make them both different objects that each inherit from MonoBehaviour. Then you can put one of each on a game object and use your finds in Start() as you've done here.

    Alternatively, leave the inheritance and understand that when you place SpawnManager on a game object, it already contains an instance of GameManager that you can access via your child SpawnManager using
    base.waveNumber;
    for example.

    I'd recommend reading up on inheritance and when to use. It doesn't look much like a SpawnManager is a GameManager in this case. They seem to be doing quite different things.

    Finally, if you choose to keep the inheritance, you will have to call base.Start() manually in your child, the SpawnManager, or GameManager.Start() will not run automatically. Things like that are easy to forget and why some recommend steering clear of inheriting from MonoBehaviour classes and just keeping seperate objects and making use of interfaces instead.
     
  3. Squatch-A-Toon

    Squatch-A-Toon

    Joined:
    Jun 27, 2020
    Posts:
    3
    Much appreciated, I knew I was getting mixed up somewhere Just needed another set of eyes to figure it out. Thanks for the help.
     
    mopthrow likes this.