0
\$\begingroup\$

I'm creating a shooter game and I'm trying to update the current count of enemies on screen. When the laser hits an enemy, it is supposed to subtract 1 from the "spawnedEnemies" and then destroy the laser. What's happening is that the spawnedEnemies count doesn't update in game, only when the game is over. Then when the game is over, it shows a negative number. When the game starts, there is 0 enemies spawned.

This is my EnemyController class. When the "Enemy" is hit with a laser, it updates the "spawnedEnemies" int in the GameManager class. But it doesn't update it in game, only when the game is over and it shows a negative number. Ex: I spawn 5 enemies, I shoot all 5 of them and it shows -6 in the Inspector.


     using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class EnemyController : MonoBehaviour
 {
     public GameManager theGM;

     // Start is called before the first frame update
     void Start()
     {
     }    

 private void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Laser")
         {
             theGM.spawnedEnemies = theGM.spawnedEnemies - 1;
             Destroy(this.gameObject);
         }
     }
 }

This is my GameManager Class:


     using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class GameManager : MonoBehaviour
 {
     public int maxEnemiesAllowed;
     public int spawnedEnemies;
     public float timeBetweenEnemies;
     public float nextEnemySpawn;

     public GameObject[] enemies;

     // Start is called before the first frame update
     void Start()
     {
         spawnedEnemies = 0;
     }

     // Update is called once per frame
     void Update()
     {
         float spawnPosition = Random.Range(-2.25f, 2.25f);

         if(spawnedEnemies <= maxEnemiesAllowed && Time.time >= nextEnemySpawn)
         {
             Instantiate(enemies[0], new Vector3(spawnPosition, 6f, 0f), Quaternion.identity);
             spawnedEnemies++;
             nextEnemySpawn = Time.time + timeBetweenEnemies;
         }
     }
 }
\$\endgroup\$
3
  • \$\begingroup\$ you have to debug your own code. its realy hard to guess what is happening to you game as no one has access to your project. in unity code is not everything so it can be from bad tag names or... \$\endgroup\$
    – virtouso
    Sep 15 '19 at 21:13
  • \$\begingroup\$ Just a guess from me, which regards class instances: make sure that the class/object that renders the enemy count on the screen is retrieving information from the same GameManager instance as the EnemyController is using. I can see that you're declaring a GameManager instance in the EnemyController and the object rendering text might not retrieve the enemy count data from that specific instance. \$\endgroup\$ Sep 15 '19 at 23:35
  • \$\begingroup\$ I've notice that you tagged this question both as C# and UnityScript, but the code you posted seems to be C#. Are you sure you need an answer which explains how to do this in the old, deprecated UnityScript scripting language? If you only need a solution for writing Unity scripts in C#, please don't use that tag. \$\endgroup\$
    – Philipp
    Sep 18 '19 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.