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 Felix-24 · Jul 08, 2016 at 11:30 PM · spawnpooling

Need a generic pooling system for shooting game

Hi I am new on Unity as well as C sharp. I wish to do a scrolling game where enemies, packs and bullets spawn randomly. I used a spawn script but I have drastical frame rates losses. I wish to generate a pooling system that can work with the Spawn script I used is that possible? And can I have a brief explanation of how a pooling system works? All the explanations I fell on were vague on their use. Spawn script it works great but creates lags : using UnityEngine; using System.Collections;

public class spawn : MonoBehaviour {

 // Use this for initialization

 public GameObject[] Prefabs;
 public float numEnemies;
 public float xLft = 19F;
 public float xRgt = 85F;
 public float yUp = 3.5F;
 public float yDwn = -4.5F;
 public float spawnCooldown = 1;
 private float timeUntilSpawn = 0;
 
 

 void Start()
 {
     //gm = GameObject.FindGameObjectWithTag("GameMaster").GetComponent<GameMaster>();
     
 }

 // Update is called once per frame
 void Update()
 {
      timeUntilSpawn -= Time.deltaTime;
  if(timeUntilSpawn <= 0)
  {
      // Do your enemy spawns here
      for (int i = 0; i < numEnemies; i++)
      {
          SpawnEnemy(); //Spawn Fxn called 
          
      }
      // Reset for next spawn
      timeUntilSpawn = spawnCooldown;
      
  }

     
 }

 private void SpawnEnemy()
 {
     //Fxn that Spawns the enemy and determines its position, Trying to make enemy prefab being produced as a child gameObject.
     GameObject enemyPrefab = Prefabs[Random.Range(0, Prefabs.Length)];
     Vector3 newPos = new Vector3(Random.Range(xLft, xRgt), Random.Range(yUp, yDwn), 0);
     GameObject action = Instantiate(enemyPrefab, newPos, Quaternion.identity) as GameObject;
     action.transform.parent = transform;
     
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Hazzanger · Jul 09, 2016 at 12:33 AM

You're going to want to have multiple states for your pooling objects. For example:

 -Active (they are visible and move or whatever) 
 -Killed or Dying (animation or dying, but can't be pooled yet)
 -Dead (invisible and ready to be revived)

All these objects are going to be in an arrayList of GameObjects, or multiple array lists for multiple enemy types. Run through the list and find which enemies are in the "Dead" phase and turn them to "Active"

Your enemies will have to have code to allow for these different states like so

   if (active){
           //patrol
     } else if (dying){
           //do the animation and once it's
           //done, turn on dead mode
     } else if (dead){
           //here, you'll make the enemy invisible,
           //and when your controller sees it, it will
           //put it back in the game
     }

and your controller will run through your arraylist of gameobjects like so:

 private void spawnEnemy(){
       int i = 0;
       while (i<enemyArray.length){
             if (enemyArray[i].GetComponent(ENEMYSCRIPTNAME).dead){
                   enemyArray[i].GetComponent(ENEMYSCRIPTNAME).dead = false;
                   enemyArray[i].GetComponent(ENEMYSCRIPTNAME).active = true;
                   //If you want to teleport, do that here
                   i = enemyArray.length;
             }
             i++;
       }
 }

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
avatar image
0

Answer by Felix-24 · Jul 10, 2016 at 11:00 PM

Ok thanks @Hazzanger I ll try that

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

60 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

Related Questions

All objects spawns with the same color but their material is different! HELP! 0 Answers

Abstracting the generic spawn function 0 Answers

Can someone post a generic object pool script? 4 Answers

How do I set up object pooling in a 2d endless runner? (New to unity) 0 Answers

Spawn Objects After Different Time Intervals 2 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