- Home /
I can't get my list to loop and keep "spawning" prefabs
Hello I had a question earlier and a user named "RudyTheDev" was a great help starting me out. The script below I got from his answer and I modified it to position my prefabs that were already instantiated at run and act like they were spawning then scrolling across the screen.
I have been trying to get it to loop the positioning process so I could essentially make an endless runner, but once the "list" fills with "activatedobjects" I can't get it to restart the process. The second script below is how I though I was emptying the list and renewing the process but it doesn't work. If I could get some help or hints on how to restart the loop it would be much appreciated.
This is the modified script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EnableRandomChildren : MonoBehaviour
{
public float spawnTime = 0.1f;
public GameObject[] spawnPoints = new GameObject[7];
// Keep a timer since the last time we "spawned" a child
private float timeSinceLastSpawn;
private int i;
void Start()
{
for(int i = 0; i < spawnPoints.Length; i++)
{
spawnPoints = GameObject.FindGameObjectsWithTag("Spawners");
}
}
void Update()
{
// Tick down time until next spawn
timeSinceLastSpawn += Time.deltaTime;
// If the 2sec have passed for spawning
if (timeSinceLastSpawn >= spawnTime)
{
// Schedule the next spawn in 2 sec
timeSinceLastSpawn -= spawnTime;
// Make a list of all children that are not yet enabled
List<GameObject> availableObjects = new List<GameObject>();
foreach (Transform child in transform)
{
if (!child.gameObject.activeSelf)
{
availableObjects.Add(child.gameObject);
}
}
// If we found some
if (availableObjects.Count > 0)
{
// Choose a random child from the list
GameObject childToEnable = availableObjects[Random.Range(0, availableObjects.Count)];
// Enable the child
childToEnable.SetActive(true);
// .. and do other things to this child
childToEnable.transform.position = spawnPoints[Random.Range(0, spawnPoints.Length)].transform.position;
}
}
}
}
And this is the script I am using to try and renew the process or reset the list in the first script which is on each of the prefabs that are activated in the script above:
using UnityEngine;
using System.Collections;
public class MoveObjectsInPool : MonoBehaviour {
public float moveSpeed = 35.0f;
public float prefabLife = 5.0f;
private float timeToReturn = 0.0f;
void Start()
{
timeToReturn = Time.time + prefabLife;
}
// Use this for initialization
void FixedUpdate ()
{
float move = moveSpeed * Time.deltaTime;
transform.Translate(-move, 0 , 0);
CountDown();
}
void Deactivate()
{
this.transform.parent = GameObject.Find("ObjectPool").transform;
gameObject.SetActive(false);
}
void CountDown()
{
if(timeToReturn < Time.time)
{
Deactivate();
}
}
}
Answer by RudyTheDev · Feb 25, 2014 at 09:58 AM
This happens because you never reset timeToReturn
. Start()
only happens once, not when you enable/disable objects. So replace your Start
with OnEnable
, so it happens every time you enable it.
Also, a few notes:
for(int i = 0; i < spawnPoints.Length; i++)
{
spawnPoints = GameObject.FindGameObjectsWithTag("Spawners");
}
should be just
spawnPoints = GameObject.FindGameObjectsWithTag("Spawners");
because GameObject.FindGameObjectsWithTag()
already returns an array. And you don't need to pre-make it with = new GameObject[7];
either. Not sure what that 7
is for, but it won't affect anything.
And private int i;
is not needed; int i
in the for loop is what defines a local variable.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134636im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
What's the best way to find the smallest Vector3.Distance of an array of enemies? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers