3
\$\begingroup\$

Hi all I have been experimenting the endless runner game for some time and needed to optimize it. I read some articles about optimizing my game and it says that once you instantiate the prefabs in the game, you should enable and disable paths at randomly so as it won't slow down the speed of the device. So I have been trying out some stuff. I managed to disable the randomly generated path but having a hard time re enabling them. Any help would be appreciated.

Here's my code and what I've tried.

TileManager.cs

public GameObject[] tilePrefabs;

private Transform playerTransform;
private float spawnZ = -19.0f;
private float tileLength = 24.0f;
private int amtOfTilesOnScreen = 5;
private float safeZone = 30.0f;


private List<GameObject> activeTiles;
private List<GameObject> deactivatedTiles;
private int lastPrefabIndex = 0;
private Vector3 transformTiles;


void Start () 
{
    activeTiles = new List<GameObject>();
    playerTransform = GameObject.FindGameObjectWithTag("Player").transform;



    for (int i = 0; i < amtOfTilesOnScreen; i++)
    {
        if (i < 2)
        {
            SpawnTile(0);
        }
        else
        {
            SpawnTile();
        }
    }
}

void Update () 
{


        if (playerTransform.position.z - safeZone > (spawnZ - amtOfTilesOnScreen * tileLength))
        {
            SpawnTile();
            DeleteTile();
        }

}

private void DeleteTile()
{
    //Destroy(activeTiles[0]);
    activeTiles[0].SetActive(false);
    activeTiles.RemoveAt(0);


}

private void SpawnTile(int prefabIndx = -1)
{
    GameObject go;
    if (prefabIndx == -1)
    {
        go = Instantiate(tilePrefabs[RandomPrefabIndex()])as GameObject;
        go.SetActive(false);
        deactivatedTiles.Add(go);

    }
    else if (prefabIndx!=-1)
    {
        go = Instantiate(tilePrefabs[prefabIndx]) as GameObject;
        go.SetActive(false);
        deactivatedTiles.Add(go);
    }        

    else if (deactivatedTiles != null)
    {
        go = deactivatedTiles[0];
        go.SetActive(true);
        go.transform.SetParent(transform);
        go.transform.position = Vector3.forward * spawnZ;
        spawnZ += tileLength;
        activeTiles.Add(go);
    }
    //activeTiles.Add(go);
    //go.SetActive(true);
    //deactivatedTiles.Add(go);
}

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;
}

Here I have pasted the code of What is needed for a solution and what Have I tried

\$\endgroup\$
  • \$\begingroup\$ When you delete a tile, you should add a reference to this tile in another list (e.g deactivatedTiles). And when you spawn, check whether this list is empty or not, if its not, pick one randomly from this list instead of instantiating a new one \$\endgroup\$ – Salepate Jul 25 '16 at 16:17
  • \$\begingroup\$ @Salepate Here I tried and added a whole code. I modified the code a bit and now comes an error--> Object reference not set to an instance of an object \$\endgroup\$ – Ashane Alvis Jul 27 '16 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.