Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help with Object Pooling

Discussion in 'Scripting' started by Amaresh, Jul 15, 2014.

  1. Amaresh

    Amaresh

    Joined:
    Jan 22, 2014
    Posts:
    21
    Hey Guys! I've made a game. It is having some performance issues in some mobile devices. It is an endless runner game where a lot of things get instantiated and destroyed. So I thought I should do something. I found the object pooling tutorial on unity live training archive. Maybe this can solve my problem.

    I understood the logic but I need a bit of help with execution. Hope you guys can help.

    I have 3 lanes. All the lanes have the Spawn Script attached to them, where it randomly generates an obstacle from the array of obstacles.

    Code (csharp):
    1.  
    2. public class SpawnScript : MonoBehaviour {
    3.  
    4.     public GameObject[] obstacles;
    5.     public float spawnMin = 1f;
    6.     public float spawnMax = 2f;
    7.  
    8.     // Use this for initialization
    9.     void Start ()
    10.     {
    11.         Spawn ();
    12.     }
    13.    
    14.  
    15.     void Spawn ()
    16.     {
    17.         Instantiate (obstacles[Random.Range(0, obstacles.GetLength(0))], transform.position, Quaternion.identity);
    18.         Invoke ("Spawn", Random.Range(spawnMin, spawnMax));
    19.     }
    20.  
    21.  
    How can I achieve this by using object pooling. If you can provide some code, it will be very helpful.

    Please Help. Thank You.
     
  2. Deleted User

    Deleted User

    Guest

    You should really just dive in and give it your best shot, you'll learn more that way...

    Teach a man to fish and whatnot...
     
  3. Amaresh

    Amaresh

    Joined:
    Jan 22, 2014
    Posts:
    21
    I've tried using the procedure in Object Pooling Tutorial. The generic class with a public method to activate or get objects. But its generating all the obstacles in a single lane stacked over one another. I just want to know how can I instantiate or pool an array of different objects. Or how can I mimic the behaviour of the above script. I thought you guys can help me out.

    Thank you.