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 /
  • Help Room /
avatar image
0
Question by kdpkke · Feb 10, 2017 at 07:10 PM · c#pooling

Object pooling with different type of enemies problem. C#

Hello guys, It's the first time I do object pooling so i'm trying to learn the basics. Until now i always instantiated and destroyed enemies when they spawned/died. I read that doing this it's not well optimized and it's better using object pooling. Enemies data are ScriptableObjects, and i've got a level (scriptableObject too) that has got every enemy that should spawn inside that level. This is what i have done until now:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
  
  
 public class EnemySpawnScript : MonoBehaviour {
     
     #pragma warning disable 0649
     private GameObject enemy;
     #pragma warning restore 0649
     public void enemySpawn (LevelData level){
         //here I loop for every ScriptableObject inside the level to instantiate the right prefabs.
         foreach (EnemyDataSpawn enemyDataSpawn in level.dataSpawner) {
             StartCoroutine (InstantiateObject (enemy, enemyDataSpawn));
         }
     }
  
     IEnumerator InstantiateObject (GameObject enemy, EnemyDataSpawn enemyDataSpawn){
         LevelManager.previousEnemyDelay += enemyDataSpawn.delay;
         yield return new WaitForSeconds(LevelManager.previousEnemyDelay);
         enemy = Instantiate(Resources.Load("EnemyPrefabs/"+enemyDataSpawn.enemy.enemyID)) as GameObject;
         enemy.name = enemyDataSpawn.enemy.enemyID;
         enemy.transform.position = enemyDataSpawn.spawnPoints;
         EnemyScript.InitializeEnemyData(enemyDataSpawn);
     }
 }

I do "LevelManager.previousEnemyDelay += enemyDataSpawn.delay" because I want that every enemy spawns with a delay (set inside the enemy ScriptableObject that now i call enemyDataSpawn) that begin when the previous enemy has spawned. Now i've studied a little about object pooling and I was able to use it for the player bullets following the unity video tutorial about pooling. The question is: how can I do object pooling with different types of enemy that should instantiate in a precise order (that can be modified in the future, so i cannot know how many enemies, what kind of enemies will spawn, etc), exactly like the code above(but using the pooling technique, without instantiating and destroying everytime)? Thank you in advance.

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
Best Answer

Answer by kdpkke · Feb 11, 2017 at 08:24 PM

In the end I found this tutorial and it works very well. It is well made and expands the unity lesson about object pooling. I suggest everyone to take a look at this!

https://www.raywenderlich.com/136091/object-pooling-unity

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawn Object in vr player hand as grabbed. Release button to let go of the object?,Spawn game object from pool into VR hand 0 Answers

Generic use of Components through typeof(Component) | Convert from Unity's generic Component into a desired one 1 Answer

Could I get an explanation of object pooling and how to do it? 2 Answers

Trouble with object pooling 2 Answers

object pooler with delayed object pools 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