Terrible FPS in simple game

Anton nelson

So I'm making a simple 2d game where the player moves on the x axis and picks up falling objects. I have a script to spawn the objects above the player, here it is:

using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour
{
    private GameObject[] locationsToSpawn;
    private float counter = 0;
    [SerializeField]
    string[] listOfPossibleTags;
    [SerializeField]
    GameObject[] objectToSpawn;
    [SerializeField]
    float timeBetweenSpawns = 3.0f;

    void Start()
    {
        locationsToSpawn = GameObject.FindGameObjectsWithTag("SpawnLocation");
    }
    void Update()
    {
        counter += Time.deltaTime;
        if (counter > timeBetweenSpawns)
        {
            GameObject spawnedObject;
            spawnedObject = Instantiate(objectToSpawn[Random.Range(0,objectToSpawn.Length)], locationsToSpawn[Random.Range(0, locationsToSpawn.Length)].transform.position, Quaternion.identity) as GameObject;
            spawnedObject.gameObject.tag = listOfPossibleTags[Random.Range(0, listOfPossibleTags.Length)];
            counter = 0;
        }
    }
}

However, after picking up just a few objects the game becomes so laggy and eventually crashes Unity! I have Intel i7, 8GB RAM, Nvidia 550m so my laptop probably isn't an issue. Also worth mentioning that my point counter goes crazy with my spawned objects, but counts objects I place manually perfectly (in other words, the spawned objects make my points counter count way too many points). Any ideas on why my game is causing low FPS? Thanks!

Neeraj Kumar

The problem seems like you are Instantiating a lot of gameobjects in "Update" at runtime and not destroying them. Ideally what you should be doing is Instantiating required gameobjects once only, store it in the objectpool and reusing it at runtime. There are lots of example available to do this, you just need to google.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related