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 Codelyy · Jan 26, 2016 at 01:58 AM · c#2dfor-loop

I can't figure out how to do this

I'm currently having problems with getting a certain part of my code working.

What I want to happen is every so often, a object will spawn and it will do this 7 times, going through each object of a array till all 7 objects have been spawned. The problem is no matter how hard I try, i can't get it to work correctly.

Here is the code:

 void ChaosE() {
         chaosTimer = chaosTimer - Time.deltaTime;
         if (chaosStart == false)
         {
             chaosStart = true;
             for (int chaosENumber = 0; chaosENumber < 7; chaosENumber++)
             {              
                 if (chaosTimer <= 0)
                 {
                     chaosENumber++;
                     Instantiate(chaosE[chaosENumber], transform.position, transform.rotation);
                     chaosTimer = 3.8f/7;
                     
                 }
             }
         }

I made the chaosStart bool true as when i didn't have it, chaosENumber will keep resetting to 0 so the for loop would keep going on and on though making chaosStart true didn't help either since now the for loop won't work at all.

I'm also having problems on trying to think of a way to do what i want, A for loop comes into mind for the thing i want but currently it doesn't work the way i want it too. I was wondering if someone would be able to help and point me in the right direction to the best way to do the task?

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
Best Answer

Answer by SterlingSoftworks · Jan 26, 2016 at 06:49 AM

When you say it "doesn't work" do you mean what's inside the if statement inside your for loop is what's not happening? If so, it will only happen one time, and sets chaosTimer > 0 on the final line.. Which then makes that if statement not happen.

Inside your for loop you shouldn't be doing the "chaosENumber++;" statement, it already does that for you, it's one of the major benefits to doing a for loop vs a while loop. Doing this can lead to the loop happening less times than you want it to.

The chaosENumber being reset to 0 every time could be that chaosStart is being turned to false somewhere outside of this function and it keeps going through the all the steps when it's called and setting it to 0.

AND FINALLY since you're wanting to spawn 7 items.. Inside the if statement inside the for loop put in a new if statement that checks if(chaosENumber == 6){ chaosTimer = 3.8f/7f; } NOW.. Once the for loop has gone through the correct amount of times THEN we reset chaos timer.

Hope this helps you out :)

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 ZefanS · Jan 26, 2016 at 07:19 AM

You're on the right track, but you're making a few mistakes that will cause strange behaviour. First of all, you shouldn't, under normal circumstances, modify the counter in a for loop manually inside the loop. You also have to keep in mind that once you enter a for loop, it won't exit until it either finishes or you explicitly tell it to stop. This means that time won't pass between each loop unless you specifically tell Unity to let that happen. Here's a script that includes two ways to achieve the behaviour you want (as far as i can tell).

 using UnityEngine;
 using System.Collections;
 
 public class SpawnStuff : MonoBehaviour
 {
     public bool oneShotMode = false;
     public GameObject objectToSpawn;
     GameObject[] spawnArray;
     float timer;
     float resetTime = 3.8f/7;
     public bool start;
     int index;
 
     void Start() 
     {
         //All objects the same for simplicity
         spawnArray = new GameObject[7];
         for (int i = 0; i < spawnArray.Length; i++)
         {
             spawnArray[i] = objectToSpawn;
         }
         timer = resetTime;
         start = false;
         index = 0;
     }
 
     void Update()
     {
         if (oneShotMode == false)
         {
             Spawn();
         }
         else
         {
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 StartCoroutine("OneShotSpawn");
             }
         }
     }
 
     void Spawn()
     {
         timer -= Time.deltaTime;
 
         if (start == true)
         {
             if (timer <= 0 && index < spawnArray.Length)
             {
                 GameObject.Instantiate(spawnArray[index], transform.position, Quaternion.identity);
                 index++;
                 timer = resetTime;
             }
         }
     }
 
     IEnumerator OneShotSpawn()
     {
         if (start == true)
         {
             index = 0;
             while (index < spawnArray.Length)
             {
                 timer -= Time.deltaTime;
 
                 if (timer <= 0)
                 {
                     GameObject.Instantiate(spawnArray[index], transform.position, Quaternion.identity);
                     index++;
                     timer = resetTime;
                 }
                 yield return null;
             }
         }
     }
 }
 

This script has two methods: Spawn() and OneShotSpawn(). Spawn() uses the Update() method to handle the timing and needs to be called every frame. OneShotSpawn() is a Coroutine and only needs to be called once. Both methods need the start variable to be true to work.

Hope this helps!

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

83 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

Related Questions

Drag 2D Sprite by Touch 1 Answer

Move GameObject to Position, Waiting, and then Moving to Another Position 5 Answers

Unity jump from one canvas scene to another scene with canvas 2 Answers

C# Script - Erro CS8025: Parsing error 1 Answer

Disable load new level until all enemies are dead 2 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