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 /
avatar image
0
Question by TheSaviour · Mar 30, 2018 at 06:54 PM · tagobjectpool

Find inactive game objects in object-pool by tag?

I've created a pool of inactive objects and in certain points of the gameplay, I want some of these inactive objects to become active only if they have a certain tag. Unfortunately, you can't access a game object by tag if it's inactive. So it seems that I won't be able to make them active when I need them.

I tried this solution:

Instead of keeping the game objects inactive in the pool, I keep their mesh-renderer turned off. And when I need them, I can just enable their mesh-renderers. However, for this I have to use the GetComponent<> function repeatedly. This works fine but from what I read in other threads, this is very inefficient.

Are there any alternatives?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by TheSaviour · Mar 31, 2018 at 04:39 PM

I've managed to find a way out of this problem. It turns out that even when an object is inactive, you can still access its transform values. Hence, you can run the Transform.childCount() method on it. So I decided to use the childCount() values as identifiers for objects in my object-pool. In other words, instead of only activating the game objects with a particular tag, I'm activating game objects with a certain childCount() value. Because in my pool, all the objects with the same tag have the same number of children. By following this strategy, I got just the results I wanted.

To be more specific, in my pool, I have 3 types of platforms: Main platform, Death platform and Unstable platform. Here's the function I created for retrieving an object from my pool:

 public GameObject RetrivePooledObject (int children)
     {
         for(int i = 0; i < platformPool.Count; i++)
         {
             if(!platformPool[i].activeInHierarchy && platformPool[i].transform.childCount == children)
             {
                 return platformPool[i];
             }
         }
 
         if (children == deathPlatformPrefab.transform.childCount)
         {
             GameObject death = (GameObject)Instantiate(deathPlatformPrefab);
             death.SetActive(false);
             platformPool.Add(death);
 
             return death;
         }
 
         if (children == unstablePlatformPrefab.transform.childCount)
         {
             GameObject unstable = (GameObject)Instantiate(unstablePlatformPrefab);
             unstable.SetActive(false);
             platformPool.Add(unstable);
 
             return unstable;
         }
 
 
         GameObject main = (GameObject)Instantiate(mainPlatformPrefab);
         main.SetActive(false);
         platformPool.Add(main);
 
         return main;
         
     }

When I eventually need a platform, I call this function from the platform spawning script by throwing the desired childCount() value as an argument.

Comment
Add comment · Show 2 · 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
avatar image Martin_Gonzalez · Mar 31, 2018 at 04:43 PM 0
Share

That's not clean. In code you should not depend on objects hierarchies. Imagine you change the child count of your objects, your code will be broken.


What i answered you was the cleaner way to access to them. No matter what change on your object, the pool will be working.


But if this work for you then continue. It's my advice (more than 5 years in Unity and coding)

avatar image TheSaviour Martin_Gonzalez · Mar 31, 2018 at 04:57 PM 0
Share

That's why I created an array of platforms in the platform spawning script. The script randomly chooses an index from the array and finds the number of children the platform in that index has. It saves that number in a variable (`int nOfChildren = platformArray[randomlyChosenIndex].transform.childCount`) and calls this function with the said number as an argument like this: GameObject spawnedPlatform = objectPool.RetrivePooledObject(nOfChildren); I probably should have mentioned beforehand that I'm actually trying to use my object pool to spawn platforms randomly. So this strategy will work even if I change the number of children for each platform (as long as I make sure that each type of platform have different number of children).

avatar image
0

Answer by Martin_Gonzalez · Mar 30, 2018 at 10:04 PM

Why don't you set a tag in the gameObject script.

Image your gameObject pool is a bullets pool and you have gameObjects with the Bullet.cs script on it.

You can set an id to that script like bulletId or bulletTag or bulletCategory. So then you can do a Where operator filtering only who has x tag.

 var myBigBullets = myInactiveBullets.Where(bullet => bullet.tag == "bigBullet");


But in this example, your lists must contains Bullet List<Bullet> myInactiveBullets instead of gameObjects.

Comment
Add comment · Show 4 · 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
avatar image TheSaviour · Mar 31, 2018 at 02:56 AM 0
Share

Alright I see where you're getting at but I feel like you need to elaborate a bit. For instance, what's the meaning of "bullet" in your code? Is it referring to the Bullet.cs script? If so, shouldn't it be capitalized?

avatar image Martin_Gonzalez · Mar 31, 2018 at 04:22 AM 0
Share

I said it: "Image your gameObject pool is a bullets pool and you have gameObjects with the Bullet.cs script on it."


Bullet is just an example

avatar image Martin_Gonzalez · Mar 31, 2018 at 02:32 PM 0
Share

I leave you a simple example.

 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 
 public class PoolExample : $$anonymous$$onoBehaviour {
 
     public int poolSize = 10;
     public GameObject myObjectPrefab;
     public List<$$anonymous$$yObject> _myInactiveObjects = new List<$$anonymous$$yObject>();
     public List<$$anonymous$$yObject> _myActiveObjects = new List<$$anonymous$$yObject>();
     
     // Use this for initialization
     void Start () {
         for (int i = 0; i < poolSize; i++) {
             var go = Instantiate(myObjectPrefab, Vector3.one * 1000, Quaternion.identity);
             var goScript = go.GetComponent<$$anonymous$$yObject>(); // be sure the prefab has the script
             _myInactiveObjects.Add(goScript);
         }    
     }
 
     $$anonymous$$yObject Get() {
         if(_myInactiveObjects.Count<=0) Debug.Log("Inactive list is empty. Add more objects to the pool");
 
         var activeObject = _myInactiveObjects[0];
         _myActiveObjects.Add(activeObject);
         _myInactiveObjects.Remove(activeObject);
 
         return activeObject;
     }
     
     $$anonymous$$yObject GetWith(string tag) {
         if(_myInactiveObjects.Count<=0) Debug.Log("Inactive list is empty. Add more objects to the pool");
 
         var customInactiveObjects = _myInactiveObjects.Where(obj => obj.myTag == tag);
         
         if(!customInactiveObjects.Any()) Debug.Log("Inactive list is empty. Add more objects to the pool");
         
         //we take the object from this list
         var activeObject = customInactiveObjects.First();
         _myActiveObjects.Add(activeObject);
         //but we remove it from the real inactive objects
         _myInactiveObjects.Remove(activeObject);
 
         return activeObject;
     }
 }
 
 //This script must be in the object prefab you want to pool
 public class $$anonymous$$yObject : $$anonymous$$onoBehaviour {
     public string myTag;
 }
avatar image TheSaviour Martin_Gonzalez · Mar 31, 2018 at 04:42 PM 0
Share

Thanks. I've actually managed to find a simpler solution to my problem. I have posted it as an answer.

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.

Follow this Question

Answers Answers and Comments

76 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

Related Questions

CharacterController high on coffee: Bounces down slopes 0 Answers

Removing and Adding gameobjects to an array upon collision 1 Answer

how do i use a raycast to hit one target to cause a explosion but hit a second target to cause a different explosion? 1 Answer

A limit to the number of tags? 2 Answers

Getcomponent through tag and findgameobject with tag and do a function but somehow gives errors (It's hard to explain) 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