More efficient Scripting.

Hello, I’m still learning C#, and I ran into whats not really a problem, but what I feel can be heavily improved upon.

  1. The objectClock clock will not be counting in 5s, that’s just for a test as the, but I still feel it could be improved. I will be adding more objects to the objectClock and change the clock variable to call the NewRandomObject function at different times.
  2. I don’t like the fact that the NewRandomObject function has to call itself over and over to find a new object if its already active. I was wondering if there’s a way to check the objects array and grab only inactive objects?

It works for what I need it to do and was just wondering if y’all had any ideas that could make it cleaner and more efficient.

using UnityEngine;
using System.Collections;

public class ObjectTimer : MonoBehaviour {

	private GameObject[] objects;
	private GameObject randomObjects;
	private int clock = 0;
	
	private void Start()
	{
		objects = new GameObject[transform.childCount];
		for (int i = 0; i < transform.childCount; i++)
		{
			objects *= transform.GetChild(i).gameObject;*
  •  }*
    
  •  StartCoroutine("objectClock");*
    
  • }*

  • private IEnumerator objectClock()*

  • {*

  •  while (true)*
    
  •  {*
    
  •  	yield return new WaitForSeconds(1);*
    
  •  	clock ++;*
    
  •  	Debug.Log (clock);*
    
  •  	if(clock == 1)*
    
  •  	{*
    
  •  		NewRandomObject();*
    
  •  	}*
    
  •  	if(clock == 5)*
    
  •  	{*
    
  •  		NewRandomObject();*
    
  •  	}*
    
  •  	if(clock == 10)*
    
  •  	{*
    
  •  		NewRandomObject();*
    
  •  	}*
    
  •  	if(clock == 15)*
    
  •  	{*
    
  •  		NewRandomObject();*
    
  •  	}*
    
  •  	if(clock == 20)*
    
  •  	{*
    
  •  		NewRandomObject();*
    
  •  	}*
    
  •  	if(clock == 25)*
    
  •  	{*
    
  •  		NewRandomObject();*
    
  •  	}*
    
  •  	if(clock == 30)*
    
  •  	{*
    
  •  		NewRandomObject();*
    
  •  	}*
    
  •  	if(clock == 35)*
    
  •  	{*
    
  •  		NewRandomObject();*
    
  •  	}*
    
  •  }*
    
  • }*

  • private void NewRandomObject()*

  • {*

  •  randomObject = objects[Random.Range(0, objects.Length)];*
    
  •  if(randomObject.activeInHierarchy == true)*
    
  •  {*
    
  •  	Debug.Log("Object already active; finding new object");*
    
  •  	NewRandomObject();*
    
  •  }*
    
  •  else*
    
  •  {*
    
  •  	randomObject.SetActive(true);*
    
  •  }*
    
  • }*
    }

This is what I would have done :

 using UnityEngine;
 using System.Collections;
 
 public class ObjectTimer : MonoBehaviour {
 
     private GameObject[] objects;
     private GameObject randomObjects;
     private int clock = 0;
     
     private void Start()
     {
         objects = new GameObject[transform.childCount];
         for (int i = 0; i < transform.childCount; i++)
         {
             objects *= transform.GetChild(i).gameObject;*

}
StartCoroutine(“objectClock”);
}

private IEnumerator objectClock()
{
for( int clock = 0 ; clock < 36 ; ++ clock )
{
yield return new WaitForSeconds(1);
if( ( clock % 5 ) == 0)
{
NewRandomObject();
}
}
}

private void NewRandomObject()
{
int startIndex = Random.Range( 0, objects.Length );
int index = 0;

// Look for the first active object starting at a random index
for ( index = startIndex ; !objects[Random.Range(0, objects.Length)].activeInHierarchy ; ++index )
{
// Prevent infinite loop
if ( index - startIndex > objects.Length )
return ;
}

randomObject = objects[index] ;
}
}