- Home /
Duplicate Question
Disable multiple game objects (Object Pooling)
Hello , I'm trying to disable some game objects that are spawned with the object pooling. The game objects that I want to disable are enemies, they must be disable when is active another game object that I have chosen.
I tried this but it does not work:
 using UnityEngine;
 using System.Collections;
 
 public class ifBossActive : MonoBehaviour {
 
     public GameObject boss;
     private GameObject[] enemies;
 
 
     void Start () {
         enemies = GameObject.FindGameObjectsWithTag ("Enemy");
     }
         
 
     void Update () {
         
         if (boss.activeInHierarchy) {
             for(int i = 0; i < enemies.Length; i++)
             {
                 enemies [i].SetActive (false);
             }
         }
 
     }
 }
This is the code for the object pooling (used for enemies):
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class enemiesGen : MonoBehaviour {
 
     public float spawnTime = 0.1f;
     public GameObject enemy;
 
     public int pooledAmount = 8;
     List<GameObject> enemies;
 
     void Start () {
         enemies = new List<GameObject> ();
         for (int i = 0; i < pooledAmount; i++)
         {
              GameObject obj = (GameObject)Instantiate(enemy);
             obj.SetActive (false);
             enemies.Add(obj);
         }
 
         InvokeRepeating ("spawnEnemy", spawnTime, spawnTime);
     }
 
     void spawnEnemy () {
 
         for (int i = 0; i < enemies.Count; i++) {
 
             if (!enemies [i].activeInHierarchy) 
             {
                 transform.position = new Vector3 (transform.position.x, transform.position.y, transform.position.z + Random.Range(50f, 250f));
                 enemies [i].transform.position = transform.position;
                 enemies [i].transform.rotation = transform.rotation;
                 enemies [i].SetActive (true);
 
                 break;
             }
         }
     }
 }
How can I fix? Thanks in advance.
Answer by gorsefan · Jun 09, 2016 at 12:14 PM
 enemies = GameObject.FindGameObjectsWithTag ("Enemy");
You do not appear to be adding the Tag to the enemies, so FindGameObjectsWithTag will find nothing :) What happens if you change to;
 enemies = GameObject.FindGameObjectsWithTag ("Enemy");
 Debug.Log (enemies.Count);
I expect it to output "0". So;
      for (int i = 0; i < pooledAmount; i++)
      {
          GameObject obj = (GameObject)Instantiate(enemy);
          obj.SetActive (false);
          obj.tag = "Enemy";
          enemies.Add(obj);
      }
I would also consider having a seperate static class with your Tag names in, so they can be referenced easily in all scripts, and your IDE can help you avoid typo-ing/forgetting the names ;)
Follow this Question
Related Questions
How do I set a reference in the inspector 1 Answer
How to active/deactive gameobject? 1 Answer
If you disable a gameobject, does an InvokeRepeating loop end or pause? 3 Answers
How to make an open and close box?,How to make open and close a box? 0 Answers
GameObject.setActive(bool) is not activating my gameObject 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                