Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TooEZGames · Sep 15, 2019 at 08:29 PM · intspawning problemsupdatingspawning-enemies

Why does my "spawnedEnemies" variable not update when enemies are destroyed?

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;
         }
     }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Anaxis_Studio · Sep 16, 2019 at 02:25 AM

My guess would be it's due to how you are handling hit detection. You're kind of using a workaround if you don't raycast for hit detection. For example, if it's possible to shoot twice before the enemy dies then that would result in a negative end score.


You could try and debug it to confirm but I can't see any other reason this would be happening.

 private void OnTriggerEnter2D(Collider2D other)
 {
     if (other.tag == "Laser")
     {
         Debug.Log("hit registered");
         theGM.spawnedEnemies = theGM.spawnedEnemies - 1;
         Destroy(this.gameObject);
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by sisse008 · Sep 16, 2019 at 01:55 PM

do you have this line:

 theGM.spawnedEnemies = theGM.spawnedEnemies - 1;


any where else in your project? maybe in OnDestroy() or OnDisable()??

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

113 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with WaitForSeconds() in a nested loop 2 Answers

How to increase the spawn rate of an object over time? 2 Answers

How to spawn enemies at different locations and avoid overlapping each other 2 Answers

Physics.CheckSphere Not Working 0 Answers

Respawn script not working! 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges