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?
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 :)
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220612061722im_/https://answers.unity.com/themes/thub/images/avi.jpg)