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 F3RILLA · Oct 26, 2015 at 10:14 PM · c#gameobjectlistlevel select

Add GameObject to List using Timer

Hey guys,

I've written a short script to add a gameobject (from an array of gameobjects) to a list of gameobjects (that will be used to spawn those gameobjects). The script works perfectly find, but I was wondering if any of you genius if this is good, I've added to many timers in one method and personally speaking, it doesn't look very good (from my point of view). Does anyone have any idea how to simplify this or an alternative way to achieve the same thing? I've recently gotten back into programming and my memory is a bit rusty.

Thank you all and hope this script may help someone in future! :)

 using UnityEngine;
 using System.Collections; //This allows the IComparable Interface
 using System.Collections.Generic;
 
 public class LevelController : MonoBehaviour 
 {
 
     // Array Stores All Platform Prefabs
     public GameObject[] Platforms;
 
     // List Stores Platforms To Be Spawned
     public List<GameObject> PlatformToSpawn;
 
     // List Containing Platforms Spawned
     public List<GameObject> PlatformSpawned;
 
     public int CurrentCount;
     public int NextCount = 5;
     
     // Use this for initialization
     void Start () 
     {
         // Execute Method to Add Platforms to Platform Spawn List
         StartCoroutine(AddPlatformTimer());
     }
 
     // Method to Add Platform to Platform Spawn List Based on Timer
     IEnumerator AddPlatformTimer()
     {
         // Execute Method to Add Platform To The List
         AddPlatformToList(); 
 
         yield return new WaitForSeconds(2);
 
         // Execute Method to Add Platform To The List
         AddPlatformToList();
         Debug.Log("1. Timer Finished Executing!");
 
         yield return new WaitForSeconds(2);
 
         // Execute Method to Add Platform To The List
         AddPlatformToList();
         Debug.Log("2. Timer Finished Executing!");
 
         yield return new WaitForSeconds(2);
 
         // Execute Method to Add Platform To The List
         AddPlatformToList();
         Debug.Log("3. Timer Finished Executing!");    
     }
 
     // Execute Method to Add Platform To List
     void AddPlatformToList()
     {
         // Increase Current Count and Add Platform to List
         for(int i = CurrentCount; CurrentCount < NextCount; i++)
         {
             // Increase Current Count
             CurrentCount++;
 
             // Add Platforms To Spawning List
             PlatformToSpawn.Add(Platforms[i]);
         }
 
         // Check if Current Count is Same as Next Count, while ensuring Next Count is not Higher than Array of Platforms
         if(CurrentCount == NextCount && NextCount < Platforms.Length)
         {
             // Increase Next Count (Number of Platform To Add)
             NextCount += 2;
             Debug.Log("Current Count is Same as Next Count!");
         }
     }
 
 }
 
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
1
Best Answer

Answer by Paulo-Henrique025 · Oct 26, 2015 at 10:43 PM

The most evident thing I can think of is using a for loop inside AddPlatformTImer

 public float platformSpawnInteral;
 IEnumerator AddPlatformTimer()
 {
     for(int i = 0; i < 3; i++)
     {
         AddPlatformToList(); 
         Debug.Log((i+1).ToString() + " Timer Finished Executing");
         yield return new WaitForSeconds(platformSpawnInteral);
     }
 }


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 F3RILLA · Oct 26, 2015 at 11:09 PM 1
Share

Thanks for the help, appreciate it bro

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

35 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

Related Questions

What is the best way to instatiate an array of connected GameObjects? 0 Answers

Subtracting the position of transform from the position of Game Objects in a list. 1 Answer

Sort list game objects based on name 3 Answers

How to select the Nth gameObject in a list and put it into a gameObject variable 1 Answer

How to select an Game Object from an Item List 0 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