Search code examples
c#unity-game-enginepooling

How to use two objects from a pooled list in one script?


I am trying to understand object pooling. I am able to get the script to pull one object at a time, but I need to be able to pull three or more from the list at the same time.

My object pooling script is big, so I don't really want to share the whole thing unless it is necessary.

I need to be able to change the location of the spawn of a flame, so I created a script to do that:

 private void CreateWavesForFlames(GameObject flame, float xBase, float xDisplacement, float dropHeight)
 {
    flame.transform.position = new Vector3(xBase + xDisplacement, dropHeight, 0);
    flame.SetActive(true); //this turn the pooled object on
} 

So I need to spawn three flames at the same time and change their spawn locations

The wave call would look something like this:

void Wave1() {
    Debug.Log("Wave1");
    tempGOHolder = gm.GetLargeFire();


    CreateWavesForFlames(tempGOHolder, 0, 0, 12);
    CreateWavesForFlames(tempGOHolder, 10, 0, 12);
    CreateWavesForFlames(tempGOHolder, 15, 0, 12);

}

What happens is only one fire flame is created and it uses the last CreatWavesForFlames. I need the three to be different.

Any suggestions on how to do this would be awesome.


Solution

  • Well.. that is what is expected from your code. if you want 3 different flame objects, Then you are going to have to do this(assuming "gm" is your pool manager object):

    tempGOHolder = gm.GetLargeFire();
    CreateWavesForFlames(tempGOHolder, 0, 0, 12);
    
    tempGOHolder = gm.GetLargeFire();
    CreateWavesForFlames(tempGOHolder, 10, 0, 12);
    
    tempGOHolder = gm.GetLargeFire();
    CreateWavesForFlames(tempGOHolder, 15, 0, 12);