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! :slight_smile:

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*);*
  •  }*
    
  •  // 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!");*
    
  •  }*
    
  • }*

}

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