- Home /
enable and disable loop objects in the scene
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[i]);
             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();
     }
 }
Answer by tyruji · Jul 22, 2021 at 12:29 PM
The problem is you are not referencing your instantiated objects, instead you are messing with prefabs in your ScriptableObject directly. What you could do is store every enemy instance in a separate array and work on that.
OK I think I understand the logic, you could give me a small example of code please?
Sure, there you go:
  public class EnemySpawner : MonoBehaviour
  {
      // ... 
      public ObjectEnemyData objectEnemyData;
      
      private GameObject[] _enemies = null;  // the array field
  
      public void Start()
      {
          // allocating the array
          _enemies = new GameObject[ objectEnemyData.enemy.Count ];
 
          for (int i = 0; i < objectEnemyData.enemy.Count; i++)
          {
              // set the enemies in your array when instantiating
              _enemies[ i ] = go = Instantiate(objectEnemyData.enemy[i]);
              go.SetActive(false);
  
          }
      }
  
      public void RandomActive()
      {
           // ... 
              int randomActive = Random.Range( 0,
                                     objectEnemyData.enemy.Count );
              _enemies[ randomActive ].SetActive( true ); 
             // and just use the instantiated game objects
      }
I deleted some lines for clarity, hope it helps.
Your answer
 
 
             Follow this Question
Related Questions
Update list on mouse click 1 Answer
Unable to make 2D enemy zigzag : Top Down View 1 Answer
2D C# destroy a GameObject on collision 2 Answers
Why is my collision script not working? 1 Answer
dash till puff game 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                