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 Slateboard · Feb 23, 2014 at 05:11 PM · beginner

How can I use Stack to Instantiate Game Objects?

I hope the title was clear, but anyways.

I've been following Unity's Space Shooter Tutorial , and I was showing it to some people in a gamedev topic.

One of them suggested that instead of calling the bullets and having them disappear upon going off-screen (which is how it works in the tutorial), it would be more efficient to instantiate a stack of bullet game objects from the start and recycle them.

At my present level, I don't fully understand how to do this, but I would like to learn, and the person who suggested this has not been available for a while so I came here.

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 robertbu · Feb 23, 2014 at 06:34 PM

Sometimes finding thins on UA is about knowing the right words. The search term is 'Object Pool' and you will find a number of posts on the subject. The idea is that instead of Instantiating and Destroying objects all the time, keep a pool of objects and reuse them. A generic pool could store a number of different object types, and when a request for a specific object type finds it is missing in the pool, the pool will create more of the item, so that (apart from something like a memory failure), a request from the pool would never fail. Objects can cycle between being used and the pool.

But for your situation and (assumed) level of expertise, let's start with something simpler. A Stack in computer programming means last in first out like a stack of dishes. Instead of a stack, I'm going to implement a circular queue so that the oldest item is always the one we get from the pool. One implemenation of a circular queue is to walk an array. When the walk reaches the end, start at the beginning again.

 #pragma strict
 
 var prefab : GameObject;  // Projectile prefab
 var maxProjectiles = 50;
 var pool : Transform[];
 var iNext = 0;
 
 function Start() {
     pool = new Transform[maxProjectiles];
     for (var i = 0; i < pool.Length; i++) {
         pool[i] = Instantiate(prefab).transform;
     }
 }
 
 function GetNextProjectile() : Transform {
     var ret = pool[iNext];
     iNext = (iNext + 1) % pool.Length;
     ret.rigidbody.velocity = Vector3.zero;
     ret.rigidbody.angularVelocity = Vector3.zero; 
     ret.gameObject.SetActive(true);
     return ret;
 }
 
 function Update() {
     if (Input.GetMouseButtonDown(0)) {
         var obj = GetNextProjectile();
         obj.position = transform.position;
         obj.rotation = transform.rotation;
         obj.rigidbody.AddForce(transform.forward * 1000);
     }
 }

Attach this script to an empty game object (or a visible one with the collider turned off). Create a prefab for your projectile. It must have a Rigidbody and be disabled. Drag and drop your projectile prefab on the 'prefab' slot. You can now infinitely fire bullets on the left mouse click without any Instantiates() or Destorys(). Add a bit of code to rotate the game object this script is attached to, and you can spray the scene.

The pool stores the Transform of the objects. It could just as easily be written to store the GameObject or the Rigidbody since you can always get from one to the other. There is no pulling anything from the pool or returning it to the pool. The pool just assumes that the oldest one is ready for resuse. This works well for projectiles that have a short lifespan.

One note with pooling. Something in either your pool code or your use code must return objects back to some initial state. For example the GetNextProjectile() set the velocity and angularVelocity. If the code did not do this, then the AddForce() would be combined with an initial velocity from the projectiles last use.

Comment
Add comment · Show 2 · 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 Slateboard · Feb 23, 2014 at 07:00 PM 0
Share

Thank you for your help. I'll try this out in the tutorial project to see if it'll work.

What are the differences between this and a C# version of the code? I understand C# better since I have more experience with it.

avatar image robertbu · Feb 23, 2014 at 10:43 PM 0
Share

Here is a C# translation:

 using UnityEngine;
 using System.Collections;
 
 public class PoolExample : $$anonymous$$onoBehaviour {
 
     public GameObject prefab;  // Projectile prefab
     public int maxProjectiles = 50;
     private Transform[] pool;
     private int iNext = 0;
     
     void Start() {
         pool = new Transform[maxProjectiles];
         for (int i = 0; i < pool.Length; i++) {
             pool[i] = (Instantiate(prefab) as GameObject).transform;
         }
     }
     
     Transform GetNextProjectile() {
         var ret = pool[iNext];
         iNext = (iNext + 1) % pool.Length;
         ret.rigidbody.velocity = Vector3.zero;
         ret.rigidbody.angularVelocity = Vector3.zero; 
         ret.gameObject.SetActive(true);
         return ret;
     }
     
     void Update() {
         if (Input.Get$$anonymous$$ouseButtonDown(0)) {
             Transform obj = GetNextProjectile();
             obj.position = transform.position;
             obj.rotation = transform.rotation;
             obj.rigidbody.AddForce(transform.forward * 1000);
         }
     }
 }

Both C# and UnityScript are procedural languages, and there is not that much difference between the two. Here is a reference on the differences:

http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html

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

19 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Material doesn't have a color property '_Color' 4 Answers

Help With Basic AI, Keep Specific Distance From Player 2 Answers

I'm using an Object Pooler for the projectiles in my game. How do I reset their movement speed when they are made active again? 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