The best way to set active game objects

Hello, I have a script here, in this game, the enemy objects are set active at different time intervals and attack the player.

Is there a better and easier way to write this script?

public int score;

public GameObject round1;
public GameObject round2;
public GameObject round3;
public GameObject round4;
public GameObject round5;

public int timeround;

public GameObject enemy1;
public GameObject enemy2;
public GameObject enemy3;
public GameObject enemy4;
public GameObject enemy5;
public GameObject enemy6;
public GameObject enemy7;
public GameObject enemy8;
public GameObject enemy9;
public GameObject enemy10;
public GameObject enemy11;
public GameObject enemy12;
public GameObject enemy13;
public GameObject enemy14;
public GameObject enemy15;
public GameObject enemy16;
public GameObject enemy17;
public GameObject enemy18;
public GameObject enemy19;
public GameObject enemy20;
public GameObject enemy21;
public GameObject enemy22;
public GameObject enemy23;
public GameObject enemy24;
public GameObject enemy25;
public GameObject enemy26;
public GameObject enemy27;
public GameObject enemy28;
public GameObject enemy29;
public GameObject enemy30;
public GameObject enemy31;
public GameObject enemy32;
public GameObject enemy33;
public GameObject enemy34;
public GameObject enemy35;
public GameObject enemy36;
public GameObject enemy37;
public GameObject enemy38;
public GameObject enemy39;
public GameObject enemy40;
public GameObject enemy41;
public GameObject enemy42;
public GameObject enemy43;
public GameObject enemy44;
public GameObject enemy45;
public GameObject enemy46;
public GameObject enemy47;
public GameObject enemy48;
public GameObject enemy49;
public GameObject enemy50;
public GameObject enemy51;
public GameObject enemy52;
public GameObject enemy53;
public GameObject enemy54;
public GameObject enemy55;
public GameObject enemy56;
public GameObject enemy57;
public GameObject enemy58;
public GameObject enemy59;
public GameObject enemy60;
public GameObject enemy61;
public GameObject enemy62;
public GameObject enemy63;
public GameObject enemy64;
public GameObject enemy65;
public GameObject enemy66;
public GameObject enemy67;
public GameObject enemy68;

void OnTriggerStay(Collider other)
{
	if (other.gameObject.CompareTag("Player"))
	{
		score = 4;
		enemy1.SetActive(true);
		enemy2.SetActive(true);
		enemy3.SetActive(true);
		enemy4.SetActive(true);
		enemy5.SetActive(true);
		enemy6.SetActive(true);
		enemy7.SetActive(true);
		enemy8.SetActive(true);
		enemy9.SetActive(true);
		enemy41.SetActive(true);
		enemy42.SetActive(true);
		enemy43.SetActive(true);
		enemy60.SetActive(true);
		enemy61.SetActive(true);
		enemy62.SetActive(true);
		enemy63.SetActive(true);
	}
}

void Update()
{
	StartCoroutine("time");

	if (timeround == 1)
	{
		round1.SetActive(true);
	}
	if (timeround == 2)
	{
		Destroy(round1);
		round2.SetActive(true);
	}
	if (timeround == 3)
	{
		round3.SetActive(true);
		Destroy(round2);
	}

	if(timeround == 4)
	{
		round4.SetActive(true);
		Destroy(round3);
	}

	if(timeround == 5)
	{
		round5.SetActive(true);
		Destroy(round4);
	}
}

IEnumerator time()
{
	if (score >= 4)
	{
		yield return new WaitForSeconds(5f);
		timeround = 1;

		yield return new WaitForSeconds(60f );

		timeround = 2;

		enemy31.SetActive(true);
		enemy32.SetActive(true);
		enemy33.SetActive(true);
		enemy34.SetActive(true);
		enemy35.SetActive(true);
		enemy36.SetActive(true);
		enemy37.SetActive(true);
		enemy38.SetActive(true);
		enemy39.SetActive(true);
		enemy40.SetActive(true);
		
		yield return new WaitForSeconds(100f );

		timeround = 3;

		enemy44.SetActive(true);
		enemy45.SetActive(true);
		enemy46.SetActive(true);
		enemy47.SetActive(true);
		enemy48.SetActive(true);
		enemy49.SetActive(true);
		enemy50.SetActive(true);
		enemy51.SetActive(true);
		enemy52.SetActive(true);
		enemy53.SetActive(true);
		enemy54.SetActive(true);
		enemy55.SetActive(true);
		enemy56.SetActive(true);
		enemy57.SetActive(true);
		enemy58.SetActive(true);
		enemy59.SetActive(true);
	    
		yield return new WaitForSeconds(145f );

		timeround = 4;

		enemy22.SetActive(true);
		enemy23.SetActive(true);
		enemy24.SetActive(true);
		enemy25.SetActive(true);
		enemy26.SetActive(true);
		enemy27.SetActive(true);
		enemy28.SetActive(true);
		enemy29.SetActive(true);
		enemy30.SetActive(true);
		enemy64.SetActive(true);
		enemy65.SetActive(true);
		enemy66.SetActive(true);
		enemy67.SetActive(true);
		enemy68.SetActive(true);

		yield return new WaitForSeconds(190f );


		timeround = 5;

		enemy10.SetActive(true);
		enemy11.SetActive(true);
		enemy12.SetActive(true);
		enemy13.SetActive(true);
		enemy14.SetActive(true);
		enemy15.SetActive(true);
		enemy16.SetActive(true);
		enemy17.SetActive(true);
		enemy18.SetActive(true);
		enemy19.SetActive(true);
		enemy20.SetActive(true);
		enemy21.SetActive(true);
	}
}

Yes. I’d start with a List.

public List<GameObject> Enemies = new List<GameObject>();

Ideally you would have something Instantiate an enemy, add it to the List, and remove it when it is dead. You probably would move away from a GameObject pretty quickly to a custom class such as so you can get info like health, damage, etc from it. That said, it depends on the game.

I wouldn’t want to manually manage 68 gameobjects.

2 Likes

Definitely time for OP to invest some skill points in C# collections (lists, arrays).

Once your stuff is in a collection, the typical process for enabling only ONE of them is more or less like this:

int whichOneToEnable = 12;

// where Enemies is a List<GameObject> type you drag in in the editor
for (int i = 0; i < Enemies.Count; i++)
{
  // only the matching one will be true
  bool active = ( i == whichOneToEnable);
  Enemies[i].SetActive( active);
}

Works for zero, one, 68 or ten million of these things, all the same code.

And yes, I agree with Lloyd: GameObjects are nice and generic but you can do better by making an array of a custom MonoBehaviour type, which lets you know these things in a more meaningful way.

1 Like

Can you show me how I can use this in this script with the same example you gave? I am new to C# and of course it doesn’t have to be 68 game objects because it takes too long. I apologize for the language, English is not my main language.

Let the enemy register itself to a collection (psedo code)

public static List<IEnemy> Lookup { get; } = new List<IEnemy>(); //TODO make it readonly for external consumers

void Awake() 
{
   Lookup.Add(this);
}

void OnDestroy()
{
   Lookup.Remove(this);
}
1 Like