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
1
Question by adilpwr11 · Jun 09, 2020 at 08:00 AM · pooling

Spawning different prefabs randomly using Object pooling

I want to spawn 3 different types of prefab randomly through pool manager script. I found the given script when I access this script in cube Spawner script but Here It doesn't take more than one prefabs sorry for weak English .If any one get my point help thanks. Dictionary> poolDictionary = new Dictionary>(); static PoolManager _instance; public static PoolManager instance { get { if (_instance == null) { _instance = FindObjectOfType(); } return _instance; } }

 public void CreatePool(GameObject prefab,int poolSize)
 {
     int poolKey = prefab.GetInstanceID();
     if (!poolDictionary.ContainsKey(poolKey))
     {
         poolDictionary.Add(poolKey,new List<GameObject>());
         for(int i = 0; i < poolSize; i++)
         {
             GameObject newObject = Instantiate(prefab);
             poolDictionary[poolKey].Add(newObject);
         }
     }
 }
 public GameObject GetObject(GameObject prefab)
 {
     int poolKey = prefab.GetInstanceID();
     if (poolDictionary.ContainsKey(poolKey))
     {
         List<GameObject> pool = new List<GameObject>();
         for(int i = 0; i < pool.Count; i++)
         {
             if (!pool[i].activeInHierarchy)
             {
                 return pool[i];
             }
         }
         GameObject newObject = Instantiate(prefab);
         poolDictionary[poolKey].Add(newObject);
         return newObject;
     }
     return null;
 }

Bellow is the script where I want to spawn different prefabs.

private void Awake() { currentPosition = cubePrefab.transform.position; } private void Start() { PoolManager.instance.CreatePool(cubePrefab, poolSize); InvokeRepeating("SpawnCubes",0f, 10f); } private void SpawnCubes() {

     GameObject cubeObject = PoolManager.instance.GetObject(cubePrefab);
     cubeObject.transform.position = currentPosition;
     cubeObject.SetActive(true);
     Vector3 newPosition = currentPosition;
     newPosition.z += distance;
     currentPosition = newPosition;
   
 }






 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ShadyProductions · Jun 09, 2020 at 05:52 PM

Use mine: https://github.com/Venom0us/UnityObjectPooler

You can pool any object of type component, including monobehaviours like gameobject. And you can retrieve custom components by ObjectPool.GetCustom(a => lamba criteria); It is documented here: https://github.com/Venom0us/UnityObjectPooler/wiki/Documentation

And there is a small sample project included.

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

126 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 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

Grabbing an object from array 0 Answers

Check if GameObjects are from the same prefab? 1 Answer

hel with object pooling? 2 Answers

In need of some help with objectpooling and randomised tag setting 0 Answers

DontDestroyOnLoad vs Hideflags.DontSave 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