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 mihnea2kx · Mar 18, 2015 at 11:54 AM · javascriptgameobjectpooling

Object pooling - SetActive not working

I have this pool manager script inspired from the unity tutorial :

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class PoolManagerScript : MonoBehaviour {
 
     public static PoolManagerScript instance;
     public bool willGrow;
     public Transform carsParent;
     public Transform scenesParent;
     public List<GameObject> cars;
     private List<GameObject> carsWithProbabilities;
     public List<GameObject> scenes;
     // Use this for initialization
 
     void Awake(){
         instance = this;
     }
 
     void Start () {
         //generate private list of cars based on their spawn probabilities and add cars to the scene
         carsWithProbabilities = new List<GameObject>();
         foreach(GameObject obj in cars){
             for(int i =1 ; i<= obj.GetComponent<SpawnedCarScript>().probability ; i++){
                 GameObject go = (GameObject)Instantiate(obj);
                 carsWithProbabilities.Add(go);
                 go.transform.parent = carsParent;
                 go.SetActive(false);
             }
 
         }
         //add scenes to the scene
         foreach(GameObject obj in scenes){
             GameObject go = (GameObject)Instantiate(obj);
             go.transform.parent = scenesParent;
             go.SetActive(false);
         }
     }
     
 
     public GameObject GetPooledCar(){
         int i = UnityEngine.Random.Range(0,cars.Count);
         if(!carsWithProbabilities[i].activeInHierarchy){
             return carsWithProbabilities[i];
         }
         if(willGrow){
             print ("growing cars");
             GameObject go = (GameObject)Instantiate(carsWithProbabilities[i]);
             go.transform.parent = carsParent;
             carsWithProbabilities.Add(go);
             return go;
         }
         return null;
     }
     public GameObject GetPooledScene(){
         int i = UnityEngine.Random.Range(0,scenes.Count);
         if(!scenes[i].activeInHierarchy){
             return scenes[i];
         }
         if(willGrow){
             print ("growing scenes");
             GameObject go = (GameObject)Instantiate(scenes[i]);
             go.transform.parent = scenesParent;
             scenes.Add(go);
             return go;
         }
         return null;
     }
 
 
 }
 

I call these functions from my generator script - THIS WORKS:

 GameObject obstacle = PoolManagerScript.instance.GetPooledCar();
 obstacle.transform.position = new Vector3(0,0,0);
 obstacle.transform.eulerAngles = new Vector3(0,0,0);
 obstacle.SetActive(true);

alt text

  • AND THIS DOESNT :

       GameObject obj = PoolManagerScript.instance.GetPooledScene();
         
         obj.transform.position = new Vector3(0,0,0);
         
         obj.transform.rotation = new Vector3(0,0,0);
         
         obj.SetActive(true);
         print ("name:" + obj.name);
         
    

Objects are not activating and the list is not growing . Prints the correct names so the function is not returning null. alt text

I have tried different approaches to activate the object but none worked . Any ideas?

img1.png (32.0 kB)
img2.png (20.6 kB)
Comment
Add comment · Show 3
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 AlwaysSunny · Mar 18, 2015 at 01:24 AM 0
Share

Consider generalizing your Pool$$anonymous$$anager so your pools/prefabs don't care "what kind of object" they contain/are. Then you only need to write pool management code once and reuse it for any pooled object, ins$$anonymous$$d of addressing e.g. cars/scenes differently.

Please address the following:

"Objects are not activating and the list is not growing." What list isn't growing? What are the clones I'm seeing in the second screenshot, if not the instances you're creating?

Pooling can be a bit confusing: $$anonymous$$ake sure you account for the possibility that the object has changed since it was created/recycled. Pooling code should include "convenience" code to reset object properties (like active state, parent, any properties you've changed which might affect recycling). It's also atypical for a pool to hold its own active creations as children.

avatar image Owen-Reynolds · Mar 18, 2015 at 02:30 PM 0
Share

Is getPooledScene returning a parent object ("Scenes" in the 2nd picture,) with inactive children?

SetActive isn't recursive. Not WRT activeSelf. The effects are, like directory permissions, or Wonder Woman's invisible plane.

avatar image webgovernor · Feb 07, 2016 at 06:25 PM 0
Share

I also have this issue.

 go = GetDisabledPoolObject(myPrefab);
 go.SetActive(true);
 Debug.Log(go.activeInHierarchy); // prints false

In fact, I can't figure out what SetActive actually does...

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Change Background Sprite at run time ? 0 Answers

Using an object pool with Javascript. 1 Answer

How to pull two objects at the same time? 0 Answers

How can i reset gameobject's properties after enabling it (like the positioning of the obstacles) if i am using object pools setting it to active and inactive? 0 Answers

How to correctly convert Object to 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