Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Bioman889 · Apr 21, 2020 at 12:56 AM · coroutinesspawning problemsmultiple objects

Spawning a set amount of game objects that replaces the objects as they're destroyed

I'm attempting to have 25 objects spawn that fall to the bottom from the top. I'm also trying to have it to where once an object is hit, another spawns at the top to take it's place. the problem I'm having with this is that the coroutine spawns as many as it pleases and floods the screen. I figure it's because the yield keeps the thing from running until two seconds have passed. is there any way that I can salvage this?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Drop_Cube : MonoBehaviour
 {
     public GameObject largeChunk;
     public Rigidbody2D largeChunkRB;
 
     public int maxCubes;
     public static float globalMaxCubes;
     public int cubeSpawnRate;
 
     // Start is called before the first frame update
     void Start()
     {
         globalMaxCubes = 0;
         largeChunkRB.AddForce(-Vector2.up, ForceMode2D.Impulse);
     }
 
     // Update is called once per frame
     void Update()
     {
         if (globalMaxCubes <= maxCubes)
         {
             globalMaxCubes += Time.deltaTime * cubeSpawnRate;
             StartCoroutine(droppingCubes());
         }
     }
 
     IEnumerator droppingCubes()
     {
         Vector3 cubeDispersion = new Vector3(Random.Range(-7f, 7f), Random.Range(-4f, 4f), Random.Range(0f, 0f));
         yield return new WaitForSeconds(1f);
         Instantiate(largeChunk, transform.position + cubeDispersion, (transform.rotation));
         Debug.Log(globalMaxCubes);
     }
 }

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

1 Reply

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

Answer by jkpenner · Apr 21, 2020 at 02:10 AM

Check to make sure that cubeSpawnRate is not set to zero in the inspector. If it is set to zero the globalMaxCubes variable will never increase and will always be below maxCubes.

The logic in your update function needs to be changed. Right know if you increase your cubeSpawnRate it could increase the globalMaxCubes count by 2 or more and only spawn one cube. This could lead to it thinking your at the max cubes, when only a few cubes are spawned.

To fix these issues I'd recommend changing your update method by using a variable for the amount of cubes spawned and another for a counter to determine when to spawn the cubes. Something similar to this:

 // Decrease a counter by the delta time.
 spawnCounter -= Time.deltaTime;
         
 // Only spawn cubes when the counter is at 0 and when
 // we are below the max allowed cube count.
 if (spawnCounter <= 0f && curSpawned < maxSpawned) {
     // Reset the spawn counter
     spawnCounter = this.spawnDelay;
         
     // Increase the current number of spawned cubes
     curSpawned++;    
     
     // Handle spawning the new cube
     StartCoroutine(droppingCubes());
 }
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 Bioman889 · Apr 21, 2020 at 02:33 AM 0
Share

thank you for the assistance, this saved me several hours of going further in the wrong direction

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

126 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 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 Coroutines and waitforseconds. 1 Answer

Trying to get my spawn manager to respond to the current score 1 Answer

Does using both Update() and a coroutine cause respawning problems? 0 Answers

Example of co-routine for documentation 1 Answer

Coroutine troubles 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