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 /
avatar image
0
Question by SolidSlish · Nov 29, 2019 at 12:34 AM · tilepoolingobject poolrunnerinsantiate

How to Use Object Pooling for a 3D Game?

So, for my project which is a Subway Surfers type game, I'm Instantiating when its clear I should be Pooling. My character moves along the Z Axis and I instantiate and then delete roughly 12 Obstacles every 5 seconds. My Draw Call is at a consistent 250 while my tris is at 144k.

How do I make a pooling system similar to my current system? Heres my current script. If possible, I want to make each tile seamlessly connect.

  using System.Collections;
  using UnityEngine;
  using System.Collections.Generic;
  
  public class TileManager : MonoBehaviour
  {
      public GameObject[] tilePrefabs;
  
      private Transform playerTransform;
      private float spawnZ = -10f;
      private float tileLength = 27.7016f;
      private float safeZone = 22.5f;
      private int amnTilesOnScreen = 4;
      private int lastPrefabIndex = 0;
  
      private List<GameObject> activeTiles;
  
      // Start is called before the first frame update
      private void Start() {
          activeTiles = new List<GameObject>();
          playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
  
          for (int i = 0; i < amnTilesOnScreen; i++)
          {
              if (i < 2)
                  SpawnTile(0);
              else
                  SpawnTile();
          }
      }
  
      // Update is called once per frame
      private void Update() {
          if (playerTransform.position.z - safeZone > (spawnZ - amnTilesOnScreen * tileLength))
          {
              SpawnTile ();
              DeleteTile ();
          }
      }
  
      private void SpawnTile(int prefabIndex = -1)
          {
          GameObject go;
          if (prefabIndex == -1)
              go = Instantiate(tilePrefabs[RandomPrefabIndex()]) as GameObject;
          else
              go = Instantiate(tilePrefabs[prefabIndex]) as GameObject;
          go.transform.SetParent(transform);
          go.transform.position = Vector3.forward * spawnZ;
          spawnZ += tileLength;
          activeTiles.Add (go);
      }
  
      private void DeleteTile()
      {
          Destroy(activeTiles[0]);
          activeTiles.RemoveAt(0);
      }
  
      private int RandomPrefabIndex()
      {
          if (tilePrefabs.Length <= 1)
              return 0;
  
          int randomIndex = lastPrefabIndex;
          while (randomIndex == lastPrefabIndex)
          {
              randomIndex = Random.Range(0, tilePrefabs.Length);
          }
  
          lastPrefabIndex = randomIndex;
          return randomIndex;
      }
  
  }
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 revolute · Nov 29, 2019 at 03:07 AM

You would not want to destroy but just disable the tiles instantiated. And you have to keep track of what are pooled. Create a dictionary for the pool like this:

 Dictionary<int, List<GameObject>> tilePool = new Dictionary<int,List<GameObject>>;

Then, in SpawnTile:

 //make sure prefabIndex is set correctly. 
 
 if(!tilePool.ContainsKey(prefabIndex)) tilePool.Add(new List<GameObject>());
 GameObject go = tilePool[prefabIndex].Find(x=>!x.activeSelf); // find disabled gameobject to reuse
 if(go == null) {
     go = Instantiate(tilePrefabs[prefabIndex]) as GameObject;
     tilePool[prefabIndex].Add(go);
 }
 //set position for go and else

finally, in DeleteTile:

 activeTiles[0].SetActive(false);//disable for reuse
 activeTiles.RemoveAt(0);

Now you have a pool for tiles.

I've wrote this here so there may be typo errors but should be easy to fix.

Comment
Add comment · Show 1 · 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 SolidSlish · Nov 29, 2019 at 06:57 PM 0
Share

Getting an error on this line on the word "Add".

How do I fix?

if (!tilePool.Contains$$anonymous$$ey(prefabIndex)) tilePool.Add(new List());

Assets\Scripts\Tile$$anonymous$$anager.cs(45,58): error CS7036: There is no argument given that corresponds to the required formal parameter 'value' of 'Dictionary>.Add(int, List)'

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

117 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

Related Questions

How to add randomness in the following code snippet? 0 Answers

Object pool help (All bullets fire at once) 1 Answer

[SOLVED] GameObject pooling is not returning enough objects! 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

Can't fire a projectile from an object pool 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