To Low FPS. What can I doo.
I've had extreme FPS crashes displaying just 50,000 low-poly cars. I have currently created an EmptyGameObject to which I have assigned a script that places the cars on the screen. Does anyone have any idea how I manage to get everything displayed with an Affordable FPS? This is my Code atm:
for (int j = 0; j < countOfCars; j++)
{
GameObject obj = Instantiate(car) as GameObject;
obj.SetActive(false);
autos.Add(obj);
}
for (int i = 0; i < autos.Count; i++)
{
if (!autos[i].activeInHierarchy)
{
autos[i].transform.position = new Vector3(-10.28f + (currentItemsInRow * abstandSeite), 0.03f, -7.65f + (rows * abstandVorne));
autos[i].SetActive(true);
fullCount++;
Debug.Log(fullCount);
if (currentItemsInRow >= countInRow)
{
currentItemsInRow = 0;
rows++;
}
else
{
currentItemsInRow++;
}
}
}
Answer by Blue-Cut · Nov 24, 2017 at 02:17 PM
Hi,
I think your problem is that you add the cars with a synchronous function. You should take a look at Unity Couroutines
In other words, your game crashes because the single frame you use to add 50000 cars needs a lot of time to do its job, when a frame is supposed to display less than a second.
Basically, the idea to fix this would be to add a few cars each update, and then wait for the next Update, until all the cars are added. The solution "looks like" something like this
private IEnumerator MyRoutine()
{
for(int i = 0; i < nbCarsPerFrame; i++)
{
if(nbCarAdded+1 < nbCarsToAdd)
{
cars.Add(myCar);
nbCarAdded++;
}
{
yield return new WaitForEndOfFrame();
}
Answer by caos23121987 · Nov 27, 2017 at 11:28 AM
Hello. The game does not crash and the cars are all displayed. Only the FPS crashes into the basement and remains even after all cars have been displayed under 10FPS
Ok, I didn't get your problem. In that case, I would think to a GPU problem. How do you initialize the materials apply on your cars?
It could be something like you have 1 material instance per car, so Unity doesn't manage to optimize rendering performances.
Here is my Code and Screenshot from the Car - Object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cars : $$anonymous$$onoBehaviour {
// Debug parameters
public float abstandSeite = 0.25f;
public float abstandVorne = 0.25f;
private int fullCount = 0;
private List<GameObject> autos;
private int rows = 0;
private int currentItemsInRow = 0;
public GameObject car;
public float intervall = 0.02f;
public int countOfCars = 50000;
public int countInRow = 100;
void Start() {
autos = new List<GameObject>();
for (int j = 0; j < countOfCars; j++)
{
GameObject obj = Instantiate(car) as GameObject;
obj.SetActive(false);
autos.Add(obj);
}
InvokeRepeating("createCar", intervall, intervall);
}
void createCar() {
if (!autos[i].activeInHierarchy)
{
autos[i].transform.position = new Vector3(-10.28f + (currentItemsInRow * abstandSeite), 0.03f, -7.65f + (rows * abstandVorne));
autos[i].SetActive(true);
// Only for Debug
fullCount++;
Debug.Log(fullCount);
if (currentItemsInRow >= countInRow)
{
currentItemsInRow = 0;
rows++;
}
else
{
currentItemsInRow++;
}
break;
}
}
}
Thank you for help