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 /
  • Help Room /
avatar image
0
Question by Dakotes · Aug 27, 2017 at 03:48 PM · c#gameobjectspawning problemsscene loadstatic variable

Spawning stops after second scene load

I am attempting to create a sort of tower defense game, where there are a certain number of spawns for each enemy type per level...Level 1 = 10 spawns, Level 2 = 12 spawns, etc. I have been using the following code for a button to restart the level.

   // Restart Button
     void Restart()
     {
         // Restart the scene
         SceneManager.LoadScene("Level 1");
         // Resume the normal game speed
         Time.timeScale = 1f;
     }  

This works as intended, so I thought I would use similar functionality to handle going between levels. The idea behind this being that I'm simply increasing the number of spawns/introducing new enemies based on the current level, while maintaining the same scene. With Debug.Log() I have been able to determine that the CurrentLevel value increases between each level, and that the following function actually runs at Start().

 // Function to repeat spawning
 void InvokeSpawn()
 {
     // Invoke the function to repeat based on the Spawn Time
     if (currentLevel <= 5)
     {
         Debug.Log("Enemy1 spawning begun");
         // Spawning Enemy 1
         InvokeRepeating("Spawn_Enemy1", spawnTime, spawnTime);
     }
 }

Unfortunately, the InvokeRepeating function never runs after going to the next level.
Here is the code for spawning the first enemy

         // Enemy 1 Spawn
         void Spawn_Enemy1()
         {
             // Randomly select the next spawn point
             int spawnPointIndex = Random.Range(0, spawnPoints.Length);
     
             // Stop spawning enemies once the counter reaches 0
             if (enemy1Spawns >= 0)
             {
                 Debug.Log("Enemy 1 spawned");
     
                 // Create the enemy at the randomly selected spawn point
                 Instantiate(enemy1, spawnPoints[spawnPointIndex].position, 
  spawnPoints[spawnPointIndex].rotation);
             }
     
             // Decrease the number of spawns
             enemy1Spawns -= 1;
     
             // Decrease total spawns
             totalSpawns -= 1;
         }

And the code when the NextLevel button is clicked

    // Next Level functionality
     public void NextLevel()
     {
         //Reset the score for the current level
         ScoreManager.score = 0;
 
         // Hide the Shop
         shopUI.SetActive(false);
 
         // Increment the current level counter by 1
         GameManager.currentLevel += 1;
 
         // Resume the game
         Time.timeScale = 1f;
 
         // Reload the scene
         SceneManager.LoadScene("Level 1", LoadSceneMode.Single);
     }

I've tried various solutions like ensuring no values are static, different methods of stopping/starting the spawn using InvokeRepeating/CancelInvoke(), and playing with the number of spawns, but nothing has worked. Everything seems to be working correctly and has the appropriate values up until it gets to InvokeRepeating("Spawn_Enemy1") functionality. The only static variables are the CurrentLevel and Score in order to track them between levels. Should that be done differently? Am I missing something here? Perhaps I'm doing something out of order or need to separate some of the functionality? Any help would be great and is always appreciated :)

Edit: I've also attempted starting the game at a higher level (4 for example), which led to the same issue. The appropriate enemies/number of enemies spawned for level 4, but the subsequent level does not spawn anything.

Comment
Add comment · Show 1
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 Genome · Aug 27, 2017 at 04:37 PM 0
Share
 // Decrease the number of spawns
     enemy1Spawns -= 1;
      
     // Decrease total spawns
     totalSpawns -= 1;

Do you ever reset the values above?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Dakotes · Aug 27, 2017 at 05:12 PM

@Genome I do not reset the values. Here is the code where they are declared

 // Public
 public int totalSpawns;
 public int enemy1Spawns = 1; 
 public int enemy2Spawns = 1;  
 public int enemy3Spawns = 1;  

totalSpawns is calculated on Awake() to get the total number of enemies for that level (enemy1Spawn + enemy2Spawn + enemy3Spawn). The enemy spawns are public so that I can modify them from the inspector, and totalSpawns is public so that it can be accessed from other scripts. I just tested and both the enemy1Spawns and totalSpawns increment correctly when the next level is loaded. I assumed that reloading the scene would reset the values since it is running a new instance of the script. Please correct me if I'm misunderstanding how it all ties together.

Edit: I figured it out. After ensuring all the GameObjects were destroyed before the scene was loaded everything worked perfectly.

Comment
Add comment · Show 1 · 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 tablekorner · Aug 27, 2017 at 08:25 PM 0
Share

So, like I said, an object was persisting between level loads?

avatar image
0

Answer by tablekorner · Aug 27, 2017 at 06:00 PM

It sounds like what's happening is that your InvokeSpawn() function is only running one time even through the scene is being reloaded because the Start() function itself is only being called once. Perhaps whatever object you have the script with that Start() function on is persisting between level loads? If that's the case, then Start() will not be called again as it's only called once in a scripts lifetime.

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

115 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 avatar image avatar image

Related Questions

Spawning a number of enemies while condition is true 0 Answers

Soccer game, more realistic net 1 Answer

Problem with destroying a gameobject 2 Answers

Fade Object Out On Scene Change 1 Answer

How can I change GameObject's texture? 1 Answer


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