I would like to create a loop of activation and deactivation of objects in the hierarchy, when the game starts instantiate all objects, then they are deactivated, and through a method should reactivate every 10 seconds.
I tried this code but it doesn’t work!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
public ObjectEnemyData objectEnemyData; //scriptableObject that contains the variable with the gameobject list
public EnemyData enemyData; //scriptableObject that contains the characteristics of the single object
public float startTimer = 0f;
public float spawnTimer = 3f;
GameObject go;
public void Start()
{
for (int i = 0; i < objectEnemyData.enemy.Count; i++)
{
go = Instantiate(objectEnemyData.enemy*);*
go.SetActive(false);
}
}
public void RandomActive()
{
startTimer += Time.deltaTime;
if (startTimer >= spawnTimer)
{
int randomActive = Random.Range(0, objectEnemyData.enemy.Count);
objectEnemyData.enemy[randomActive].SetActive(true);
startTimer = 0f;
Debug.Log(randomActive);
}
}
private void Update()
{
RandomActive();
}
}