Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 serenefox · Feb 25, 2014 at 03:34 AM · c#listloop

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();
         }
     }
 }
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 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.

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 serenefox · Feb 25, 2014 at 10:36 PM 0
Share

Thanks, I didn't know OnEnable was an actual function.

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

21 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

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

Can't add multiple integers to a List in a Loop! 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