Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jun 10, 2016 at 10:47 PM by BDR530 for the following reason:

Duplicate Question

avatar image
0
Question by BDR530 · Jun 07, 2016 at 10:55 PM · disableactiveenemiesobject pool

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0
Best Answer

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 ;)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges