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 *.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 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 .activeInHierarchy)_
* {*
* transform.position = new Vector3 (transform.position.x, transform.position.y, transform.position.z + Random.Range(50f, 250f));*
_ enemies .transform.position = transform.position;
enemies .transform.rotation = transform.rotation;
enemies .SetActive (true);_

* break;*
* }*
* }*
* }*
}
How can I fix?
Thanks in advance.

Seems like you have to export the solution from unity and build it in visual studio if you are developing for Hololense (see [example][1]). [1]: https://developer.microsoft.com/en-us/windows/holographic/holograms_100

1 Answer

1

enemies = GameObject.FindGameObjectsWithTag (“Enemy”);
You do not appear to be adding the Tag to the enemies, so FindGameObjectsWithTag will find nothing :slight_smile: 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 :wink: