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 ZSketcher · Apr 19, 2020 at 03:24 AM · spawningspawning problemsspawnerspawn points

Why doesn't my spawning code work?

I want it to spawn balloons in a set distance between each other in rounds a set time apart. but the balloons spawn on top of each other and aren't spacing themselves apart.

 public GameObject red;
 public GameObject yellow;
 public GameObject green;
 public GameObject blue;

 public int numberOfReds;

 private bool timerOn;
 private float timer;
 public float secondsBetweenWaves;

 private float distance;
 public float distanceBetweenBalloons;


 void FixedUpdate()
 {
     Timer();
     if (timer <= 0)
     {
         for (int i = 0; i < numberOfReds; i++)
         {
             Instantiate(red, transform, false);
             distance = distanceBetweenBalloons;
             while(distance > 0 && i != numberOfReds - 1)
             {
                 DistanceTimer();
             }
         }
         timer = secondsBetweenWaves;
         timerOn = true;
     }
 }
 void Timer()
 {
     if (timerOn)
     {
         timer -= (Time.deltaTime);
         if (timer <= 0)
         {
             timerOn = false;
         }
     }
 }


 void DistanceTimer()
 {
     distance -= (Time.deltaTime);
 }

Comment
Add comment · Show 4
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 logicandchaos · Apr 19, 2020 at 04:52 PM 0
Share

you should be using update not fixed update, fixed update is for physics updates, you also never assign distanceBetweenBalloons..

avatar image ZSketcher logicandchaos · Apr 19, 2020 at 07:05 PM 0
Share

I will fix th Update method, and I am currently using the editor to assign distanceBetweenBalloons for testing, eventually I plan to write a script that will have rounds that will change it.

avatar image endasil_unity · Apr 20, 2020 at 06:35 AM 0
Share

Is secondsBetweenWaves set to the correct number? I would do a debug log after setting timer to see the value and also as the Timer update to see how it decreases.

avatar image ZSketcher endasil_unity · Apr 20, 2020 at 09:04 PM 0
Share

The values seem to be outputting as would be expecting, so I believe the error occurs with the balloons spawning at the wrong time.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by bombombambam · Apr 20, 2020 at 10:13 PM

The while loop doesn't run each frame, it runs much faster and so the distance is instantly 0, you should better add a delay using maybe a coroutine.

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 unity_ek98vnTRplGj8Q · Apr 20, 2020 at 10:37 PM

  1. You aren't using your distance variable anywhere, instead every balloon is being put at the position of the object that has this spawner script attached.

  2. Your DistanceTimer() function is not doing anything. The while loop will run fully through every time FixedUpdate is called, so you are setting distance and then immediately lowering it to zero in a while loop (I imagine you wanted this while loop to work as a time delay between each balloon spawn? Otherwise I'm not sure what the purpose of it is)...


There are lots of ways you could implement this (Coroutines would probably be better than what I'm about to suggest), but here is a script that I think loosely does what you are trying to do. I had to make some assumptions about how you wanted this to work so you will have to go in and tweak it to fit your goals, but this should get you started. Note that I have not tested this code.

 public GameObject red;
 public float timeBetweenWaves;
 public float distanceBetweenBalloons;
 public float timeBetweenEachBallon;
 public int numberOfReds;
 
 private bool spawningWave = false;
 private float waveTimer = 0; //Tracks time between waves
 private float spawnTimer = 0; //Tracks time between balloons
 private int numberRedsSpawned = 0;
 
 void Update () {
 
     if (!spawningWave) {
         waveTimer -= Time.deltaTime;
         if (waveTimer <= 0) spawningWave = true;
     }
 
     if (spawningWave) {
         if (numberRedsSpawned >= numberOfReds) {
             //Done spawning red balloons
             numberRedsSpawned = 0;
             spawningWave = false;
             waveTimer = timeBetweenWaves;
             spawnTimer = 0;
         } else {
 
             
             spawnTimer -= Time.deltaTime;
             if (spawnTimer <= 0) {
                 //Spawn a balloon
                 //First calculate the distance offset
                 Vector3 direction = Vector3.right; //Not sure what direction you want the balloons to be spawned in, so lets just use the x axis
                 Vector3 spawnOffset = numberRedsSpawned * distanceBetweenBalloons * direction;
                 Instantiate (red, transform.position + spawnOffset, Quaterion.identity, transform);
                 numberRedsSpawned++;
                 spawnTimer = timeBetweenEachBallon;
             }
         }
     }
 }
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

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

Related Questions

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

How to spawn all humans at once and activate then with time? 1 Answer

How to prevent multiple spawns from a single spawn point? 1 Answer

My game object that spawns randomly threw out my game sometimes spawns inside a tree 0 Answers

Spawning help 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