Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Mikenseer · Jan 06, 2015 at 08:02 PM · objectlistobjectsactivecount

Counting active objects in a list

Utilizing the idea of object pooling, and storing said objects in a list. As seen in here: Object Pooling

I would like to run a check on that list to see if there are any objects active. I know you can utilize GameObject.activeInHierarchy on singular objects such as this Spawn() code:

 List<GameObject> objectQueue;

     void Spawn () {
     
         for (int i = 0; i < objectQueue.Count; i++) {
 
             if(!objectQueue[i].activeInHierarchy)
             {    
                 DetermineSpawnLocation(objectQueue[i]);  //Decide where to spawn
                 objectQueue[i].SetActive(true);          //object spawning
                 break;
             }
 
         }
 
     }

I would like to do this check in a separate function as this Spawn code is called after some checks are made, and one of the checks I need to add is 'if there are any active shapes, wait for the count to be 0' (no active shapes in scene) before continuing. I was considering a foreach or for loop and manually counting active objects, but it seems there should be a better way.

If this question has be answered before please link it and reprimand me properly. Apologies if the answer is crazy simple.

TL;DR: How to count number of active objects within a list in simplest way possible.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by george_vasilchenko · Jan 06, 2015 at 10:22 PM

Hello, here is a script that creates objects, puts them in a list, on left mouse click deactivates random amount of them and on the right mouse click counts remaining active instances. Hope that's what you needed. EDIT: if you click a scroll button, it prints out names of your active objects... Why do u need to use activeInHierarchy???

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

 public class SpawnObjectsCountActive : MonoBehaviour 
 {
     int numActiveObj = 0;
     int objToSpawn = 10;
     public GameObject yourObject;
     [SerializeField]
     List<GameObject> yourObjects = new List<GameObject>();
     List<string> namesOfActive = new List<string>();

     void Start()
     {
         for (int i = 0; i < objToSpawn; i++)
         {
             GameObject aClone = Instantiate(yourObject, new Vector3(0, 0.5f + i, 0), Quaternion.identity) as GameObject;
             aClone.transform.parent = transform;
             aClone.name = "aClone_" + i;
             yourObjects.Add(aClone);
         }
     }
     void Update()
     {
         if (Input.GetMouseButtonDown(0)) RandomDeactivate(Random.Range(1, 9));
         if (Input.GetMouseButtonDown(1)) Debug.Log(CountActive());
         if (Input.GetMouseButtonDown(2)) PrintNamesOfActive();
     }
     private int CountActive()
     {
         for (int i = 0; i < yourObjects.Count; i++)
         {
             if (yourObjects[i].activeSelf == true)
             {
                 numActiveObj++;
                 namesOfActive.Add(yourObjects[i].name);
             }
         }
         return numActiveObj;
     }
     private void PrintNamesOfActive()
     {
         int count = 1;

             foreach (string name in namesOfActive)
             {
                 Debug.Log(string.Format("obj#{0}:{1} is active", count, name));
                 count++;
             }
     }
     private void RandomDeactivate(int amount)
     {
         for (int i = 0; i < amount; i++)
         {
             yourObjects[i].SetActive(false);
         }
     }
 }

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 Mikenseer · Jan 06, 2015 at 10:27 PM 0
Share

This looks awesome. Similar to the route i was thinking of taking.
Barring that there is no simple yourObjects.activeInHiearchy.Count() method, this solution looks stellar.

EDIT: No need to use activeInHiearchy, was just a rough idea of how I imagined things could work if the function was built into unity. Sorry I should have been more clear :P

avatar image Mikenseer · Jan 07, 2015 at 04:57 PM 0
Share

EDIT: I Have solved my issue, not really using anything from this thread, but hey, the question didn't exist before so maybe this will help someone in the future!

avatar image
0

Answer by hexagonius · Jan 06, 2015 at 10:05 PM

Sorry for not answering your particular question, but why not have two lists, one holding active, one holding inactive objects. Move them over whenever a change occurs. Then you only need to check the count.

Btw. If all are identical objects, queue is actually a nice alternative to list, check it out.

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 Mikenseer · Jan 06, 2015 at 10:12 PM 1
Share

The objects are identical, but their children can change. Would this break the use of a queue?

Thanks for the answer! I will add it to my notes for sure

avatar image DanSuperGP · Jan 06, 2015 at 10:20 PM 1
Share

Adding and removing objects from a list isn't very efficient, especially if they are in the middle.

Iterating through the list and counting active members would be much faster.

avatar image hexagonius · Jan 06, 2015 at 10:26 PM 1
Share

Depends on what you do with them. As soon as you need to pick a particular object you'll need a list. But if it's just a bunch of the same objects you just need one of, queue or stack are better ideas. The difference between those two is, with the queue you get the first object put in, with a stack the last (FIFO an LIFO, first or last in first out).

Its a question of accessibility. But list is fine :)

avatar image DanSuperGP · Jan 06, 2015 at 11:26 PM 1
Share

Right, but the use case at the top says "object pooling"

Generally when you're doing object pooling you have objects going active and inactive at different times, you're losing a lot of efficiency by adding them to separate lists every time they go active and inactive. It's better to just put them all in a big list and when you need a new one, iterate through the list until you find an inactive one and return it.

avatar image
0

Answer by DanSuperGP · Jan 07, 2015 at 12:33 AM

Just loop through the list and count actives. It's simple, it's efficient. The whole point of lists is fast consecutive access.

You could do other things like use a linq query to get only the active objects from the collect, but that's going to be slower.

There's nothing wrong with the simple obvious way of doing this.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Count >= then object tag becomes active 1 Answer

A node in a childnode? 1 Answer

counting of two different objects 0 Answers

Get Count of a List inside a List / Class 1 Answer

ObjectPicker Source Code? 0 Answers


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