2

Alright, Im working in Unity in C# and Ive created a few simple functions to spawn a series of platforms (game objects) indefinitely in accordance with the z position of the player. I delete the game objects once they are securely out of sight and called this update method every time player moves forward:

void spawnSectorGroup()
    {
        int i = numSectorsToSpawn; //get position of last sectors, when its exceeded by player then delete and spawn again
        while (i >= 0) {
            int j = Random.Range (0, sectors.Length);
            spawnSector (gamePosition.position, sectors [j]);
            i--;
        }
    }
    void checkAndUpdateSectors() 
    {
        //print ("Player pos"+playerPosition.position);
        //print ("last sector"+gamePosition.position);

        if (gamePosition.position.z - playerPosition.position.z <= 20) { //safe value ahead
            print ("theyre almost there, spawn new group");
            print (gamePosition.position.z - playerPosition.position.z);
            spawnSectorGroup ();

        }

        //destroy past ones
        GameObject[] objects = FindObjectsOfType(typeof(GameObject)) as GameObject[];
        //GameObject[] objects = GameObject.FindGameObjectsWithTag("Block");
        foreach (GameObject o in objects) {

            if (o.gameObject.tag == "Block" || o.gameObject.tag == "Cage" || o.gameObject.tag == "Animal" || o.gameObject.tag == "Enemy") {

                if (playerPosition.position.z - o.transform.position.z >= 100) { //safe aways back

                    print ("destroying past object");
                    print (o.transform.position);
                    Destroy (o.gameObject);
                }
            }

        }

    }
void spawnSector(Vector3 relativePosition,GameObject sector){
        GameObject newSector = GameObject.Instantiate (sector, relativePosition, transform.rotation) as GameObject;
        gamePosition.position += newSector.GetComponent<Sector> ().length;
    }

This all works, however Im worried in the long run if this spawning of about 10 new "sectors" each time the player is within 20 or so units of the last spawned sector will build up and create a lag from the multitude of game objects.

Im not experienced with indefinite spawning - will this be a problem? Or is this good practice with indefinite spawning?

blue
  • 7,175
  • 16
  • 81
  • 179

1 Answers1

7

Creating and destroying objects is expensive and something you want to avoid doing in large amounts while your game is running.

Check out this Unity tutorial on Object Pooling. The basic idea is that instead of creating and destroying the objects, you take them from a pool of existing objects and return them when you are done so they can be reused.

CaTs
  • 1,303
  • 10
  • 16
  • Thanks - with create/destroying will the lag be noticeable? How much is a large amount? – blue Nov 04 '16 at 15:50
  • 1
    It all depends on the complexity and amount of objects. Get it going, play with it and then decide if it needs optimising! – CaTs Nov 05 '16 at 07:51