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 sdgd · Feb 23, 2013 at 08:22 PM · c#gameobjectpoolingadvice

GameObject Pool heaving difficulties getting a pool at all trying to find different aproach

any small pushes how should I approach this will be helpful thanks in advance

problem is not within the code thing is I wanted to go not to destroy and istantiate a new GO at all

but as soon as I moved to figureing what kind of GO are actually falsed I figured out I'm already destroying a GO so doing all complex for nothing

problem is the first one might never get falsed and the last one will or the other way around

this are not bullets but bricks or what ever that are false ONLY when player picks them up

yes this works smoothly and all but I think it's even less effective than just destroying a GO and instantiating new one

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 // TRAINING PURPOSES ONLY
 
 public class Pooling : MonoBehaviour {
     public GameObject prefab ;
     public List<GameObject> ActiveCopy ;
     public List<GameObject> DeactiveCopy ;
     public float time = 0;
     public float time1 = 0;
     public float timeremaining = 0.1f ;
     
     public bool NewOne = false ;
     
     void Update (){
         if (! NewOne){
             time = Time.time ;
             NewOne = true ;
         }
         time1 = Time.time ;
         
         if ((time1 - time) > timeremaining){
             
             ActiveCopy.Add(prefab);
             
             ActiveCopy[ActiveCopy.Count-1] = Instantiate( ActiveCopy[ActiveCopy.Count-1] ) as GameObject;
             ActiveCopy[ActiveCopy.Count-1].transform.position = transform.position ;
             ActiveCopy[ActiveCopy.Count-1].transform.parent = transform ;
             
             Deactivate(ActiveCopy.Count-1);
             NewOne = false ;
         }
     }
     
     void Deactivate (int Count) {
         ActiveCopy[Count].SetActive(false) ;
         DeactiveCopy.Add(ActiveCopy[Count]) ;
         ActiveCopy.Remove(ActiveCopy[Count]) ;
     }
 }


I didn't create with

 public GameObject[] Copy

because every time I do than

 Copy = new GameObject[(IncreasedNumber)]

all GO are simply deleted within Copy and Copy does not contain them any longer while they exist in scene and I can't track them

I thinked a bit more and heard some times about taggs I don't even know how does tagging work

all I want is find the object that's false it doesn't matter first false or last false one but defenally I don't want to pick up the true one

they will not get false over time but randomely by player when player picks it up

Comment
Add comment · Show 9
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 theundergod · Feb 24, 2013 at 12:18 AM 0
Share

If I'm reading your code correctly it looks like your are setting every object to SetActive(false) right when they are instantiated. Is there a reason for this? $$anonymous$$aybe I don't understand what you are trying to accomplish.

avatar image sdgd · Feb 24, 2013 at 12:19 AM 0
Share

am yes I'm just setting them to false right away I didn't want to bother with 20 more lines of code for learning purposies

but the thing is when I want to create them false can change in future but the problem is I'm looking from wrong angle as I need to destroy/create new GO again

just to make sure all destroyed are in 1 place so I don't need to search for them when creating later

(yeah didn't even went that far with code because I'm at wrong angle already) and searching through web for solution

I was thinking somehow tagging but I have no knowledge about tagging

I could get in a few ifs for that function or when that functon is called (when player clicks on object) but all that is already done and don't need to focus there

I'm focusing not to making a garbage collection work too much

avatar image theundergod · Feb 24, 2013 at 12:26 AM 0
Share

I think there may be a bit lost in translation here. When you say "destroyed" you mean you've SetActive(false)? Not actually destroyed the GO but simply deactivated it? And when you reference an index in DeactiveCopy like: DeactiveCopy[index] it returns a NULL value?

avatar image sdgd · Feb 24, 2013 at 12:49 AM 0
Share

yes when I give ActiveCopy.Remove(ActiveCopy[Count]) ;

is same as I'm doing destroy GO isn't it? (not as a code GO still exists but garbage collector)

and when I got there I said Oh I actually achieved nothing

this script is learning purpose how not to destroy a GO but heaving a pool

atm I'm destroying GO with other scripts not this one

I was thinking giving it to different array so I could easily find falsed ones and when I saw what I've done I saw I need different aproach and for this aproach I'm asking you guys I don't have a clue

avatar image theundergod · Feb 24, 2013 at 01:16 AM 1
Share

Ah so you don't want to have to use DeactiveCopy at all then? So you want one array with "Active" and "Deactive" GameObjects but be able to deter$$anonymous$$e which ones within are active and which ones are not?

Show more comments

1 Reply

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

Answer by theundergod · Feb 24, 2013 at 01:25 AM

First instantiate your objects and put them in the list however you'd like.

Example:

 List<GameObject> gameObjects = new List<GameObject>();
 
 //instantiate 20 GameObjects and set every other one to inactive
 for(int x = 0; x < 20; x++)
 {
     //add an instantiated GameObject
     gameObjects.Add((GameObject)Instantiate (prefab, prefab.transform.position, prefab.transform.rotation));
     
     //if a round number then set that object to inactive
     if(x%2 == 0)
     {
         gameObjects[x].active = false;
     }
 }

There are a couple ways to find the inactive objects

1 - Loop through and check the Active state.

 foreach(GameObject go in gameObjects)
 {
     if(go.active)
     {
         //do something with the active GameObject
     }
     else
     {
         //do something with the inactive GameObject
     }
 }

2- Using Linq in C#

 IEnumerable<GameObject> inactiveGameObjects = System.Linq.Enumerable.Where(gameObjects, n => n.active == false);
 
 foreach(GameObject go in inactiveGameObjects)
 {
     Debug.Log(go.name);
 }

3- Using Tags in Unity Set the GameObjects tag by using

 yourObject.tag = "TagName";

Then get all GameObjects with tag "TagName" using

 GameObject[] inactiveGameObjects = GameObject.FindGameObjectsWithTag("TagName");

Be sure to add the Tag in the Tag/Layer editor in Unity or Unity won't recognize it as a valid tag.

Comment
Add comment · Show 15 · 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 theundergod · Feb 24, 2013 at 01:27 AM 0
Share

I'll update in a second with more concise code if you'd like.

avatar image sdgd · Feb 24, 2013 at 01:30 AM 0
Share

am are 1 and 2. separate and not 1 for another if so

can you explain more about 2. one?

I didn't have in $$anonymous$$d to loop through all objects

avatar image theundergod · Feb 24, 2013 at 01:36 AM 1
Share

There are many ways to accomplish this but if you'd like to learn more about Linq check out this reference: http://msdn.microsoft.com/en-us/library/bb397933.aspx

avatar image theundergod · Feb 24, 2013 at 01:39 AM 1
Share

In order to understand the

 n => n.active = false

I would recommend looking at http://msdn.microsoft.com/en-us/library/bb397687.aspx

Lambda expressions can be very powerful although I think we may be getting way out of scope of your particular exercise.

avatar image theundergod · Feb 24, 2013 at 01:44 AM 1
Share

Ah I was going a little overboard then. If you definitely want to use tags you can simply set the tag using

 yourObject.tag = "TagName";

Then use

 GameObject[] inactiveGameObjects = GameObject.FindGameObjectsWithTag("TagName");

Then cycle through the inactiveGameObjects array.

Show more comments

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

10 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

Related Questions

Pooling Efficiency Transform vs gameObject 0 Answers

Multiple Cars not working 1 Answer

How to find all GameObjects in a parent in shorter code than I figured out 1 Answer

How can I LERP the X position to the X position of the player 2 Answers

Distribute terrain in zones 3 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