• 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 /
avatar image
Question by Arcsharp · May 02, 2019 at 12:31 PM · object poolconditional

How can I prevent active gameobjects from enqueueing to my object pool?

So I've been fiddling around with my object pool as I've noticed that objects that stay on the screen too long deactivate and reactive w$$anonymous$$ch is problematic since the projectiles should be only be disabled by the players interaction or if they $$anonymous$$t the health object. I have a general idea of where I'm supposed to fix it but I'm not sure how to type it. T$$anonymous$$s is what I got so far.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ArrowSpawner : MonoBehaviour { [System.Serializable] public class Pool { public string tag; public GameObject prefab; public int size; }

 public Transform[] SpawnPoints;
 public GameObject[] ArrowReflector;
 public static int arrowDetectSpawn;
 public static Vector3 arrowDetectObj;
 public static GameObject OriginatedFrom;
 public static int arrowNumber = 0;
 public GameObject[] Arrow;
 int randomSpawnPoint, randomArrow;
 public bool spawnAllowed;
 public List<Pool> pools;
 public Dictionary<string, Queue<GameObject>> poolDictionary;
 public static List<GameObject> arrowList = new List<GameObject>();

 public float spawnSpeed = 1f;
 public float spawnStart = 0f;
 public static GameObject obj = null;
 private Queue<GameObject> temp;

 void Start()
 {
     poolDictionary = new Dictionary<string, Queue<GameObject>>();

     foreach (Pool pool in pools)
     {
         Queue<GameObject> objectPool = new Queue<GameObject>();

         for (int i = 0; i < pool.size; i++)
         {
             GameObject obj = Instantiate(pool.prefab);
             obj.SetActive(false);
             objectPool.Enqueue(obj);
             obj.name = "arrow" + arrowNumber;
             arrowNumber++;
             arrowList.Add(obj);
         }
         poolDictionary.Add(pool.tag, objectPool);
     }

     if(spawnAllowed == true)
     {
         InvokeRepeating("SpawnArrows", spawnStart, spawnSpeed);
     }
 }

 void Awake()
 {
     var arrowFind  = GameObject.FindWithTag("Arrow");
     if (arrowFind)
     {
         Destroy(Arrow[randomArrow]);
     }


 }

 public void Update()
 {
    /* for (int i = 0; i < 10; i++)
     {
         Debug.Log(arrowList[i]);
     }*/
 }

 public GameObject SpawnFromPool (string tag, int position, Quaternion rotation)
 {
     GameObject objectToSpawn = poolDictionary[tag].Dequeue();

     objectToSpawn.SetActive(true);
     randomSpawnPoint = Random.Range(0, SpawnPoints.Length);
     objectToSpawn.transform.position = SpawnPoints[randomSpawnPoint].transform.position;
     objectToSpawn.transform.rotation = rotation;
     arrowDetectSpawn = randomSpawnPoint;
     arrowDetectObj = objectToSpawn.transform.position;
 
     /*
      if (object in poolDictionary.isActive)
      {
         Don't Enqueue said object.
      }
      else 
      {
         Lets add it to the queue.
      }

      */
     poolDictionary[tag].Enqueue(objectToSpawn);
     return objectToSpawn;
 }


 void SpawnArrows()
 {
     if (spawnAllowed)
     {
         
         
         SpawnFromPool("Arrow", randomSpawnPoint, Quaternion.identity);
     }
 }
 

}

Any help would be much appreciated.

Comment

People who like this

0 Show 1
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
avatar image theunsigned · Jan 31, 2020 at 11:08 PM 0
Share

I'm having a similar issue after using Brackeys' youtube tutorial. Did you find a solution? I like the use of a tag vs. other list-based pooling options that use indexes to reference the pooled objects and would like to figure it out too.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

104 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Key Combo system. where I am going wrong with this? 2 Answers

Object pool - how to load stuff 1 Answer

Object pooling with lightmapped gameobjects 0 Answers

Platform generation using object pooling 0 Answers

Switch-case with enum 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