Hello, it seems that I am having an issue where my generator object will spawn in 500 trees, and by the time it is done, the game is lagging really bad. I have seen plenty of games where more than this amount of objects exist and the game still runs fine, plus these are low-poly models. Also how do I allow Unity to use more resources than it is currently using?
Code for script that is in tree:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TreeAIScript : MonoBehaviour {
private GameObject Tree;
private bool SaplingStage;
private bool JuvStage;
private bool AdultStage;
private bool ElderStage;
private bool AncientStage;
private float TreeAge;
public float TreeAgeRate;
private float CurrentSize;
public float AbTreeSize;
public float GrowthRate = 1;
public float StartSize;
public float EndSize;
public float SetSize;
public GameObject Fruit;
public float FruitingRate;
private float Energy;
private float MaxEnergy;
private bool EnergyOverflow;
public float EnergyUseRate = 1;
public float SetMaxEnergy = 1;
public float EnergyGainRate = 1;
private Vector3 VectorSize;
public float FruitTimer;
public float FruitTimerMax;
private Vector3 FruitSpawnLoc;
private float FruitSpawnx;
private float FruitSpawnz;
private float FruitSpawny;
void Start () {
Tree = gameObject;
SaplingStage = true;
Tree.name = "Sapling";
MaxEnergy = 10;
CurrentSize = 0.1f;
Energy = MaxEnergy / 1;
}
void Update () {
FruitTimer += FruitingRate;
FruitTimerMax = 120 / GrowthRate;
VectorSize = new Vector3 (SetSize * (CurrentSize), SetSize * (CurrentSize), SetSize * (CurrentSize));
transform.localScale = VectorSize;
TreeAge += Time.deltaTime;
if (Energy > MaxEnergy) {
Energy = MaxEnergy;
}
MaxEnergy = SetMaxEnergy * (CurrentSize * 100);
Energy += EnergyGainRate * (MaxEnergy / 300);
if (SaplingStage == true){
if (Energy / MaxEnergy > 0.10f) {
CurrentSize += 0.1f * ((GrowthRate/10000)/CurrentSize);
Energy += -CurrentSize * 0.01f;
if (CurrentSize >= 0.3f) {
SaplingStage = false;
JuvStage = true;
Tree.name = "Young Tree";
}
}
}
if (JuvStage == true) {
if (Energy / MaxEnergy > 0.10f) {
CurrentSize += 0.15f * ((GrowthRate/10000)/CurrentSize);
Energy += -CurrentSize * 0.001f;
if ((CurrentSize >= 1)) {
JuvStage = false;
AdultStage = true;
Tree.name = "Tree";
}
}
}
if (AdultStage == true){
if (Energy / MaxEnergy > 0.20f && FruitTimer >= 120) {
FruitSpawnx = Tree.transform.position.x + Random.Range ((CurrentSize) * -2, (CurrentSize) * 2);
FruitSpawnz = Tree.transform.position.z + Random.Range ((CurrentSize) * -2, (CurrentSize) * 2);
FruitSpawny = Tree.transform.position.y + (CurrentSize) * 3;
FruitSpawnLoc = new Vector3 (FruitSpawnx, FruitSpawny, FruitSpawnz);
Instantiate (Fruit, (FruitSpawnLoc), Quaternion.identity);
FruitTimer = 0;
Energy += -10;
}
if (Energy / MaxEnergy > 0.30f) {
CurrentSize += 0.15f * ((GrowthRate/10000)/CurrentSize);
Energy += -CurrentSize * 0.001f;
if (CurrentSize >= 3) {
AdultStage = false;
ElderStage = true;
Tree.name = "Great Tree"; }
}
}
if (ElderStage == true){
if (Energy / MaxEnergy > 0.50f) {
CurrentSize += 0.2f * ((GrowthRate/10000)/CurrentSize);
Energy += -CurrentSize * 0.01f;
if (CurrentSize >= 10) {
ElderStage = false;
AncientStage = true;
Tree.name = "Ancient Tree";
}
}
}
if (AncientStage == true){
if (Energy / MaxEnergy > 0.90f) {
CurrentSize += 0.15f *((GrowthRate/10000)/CurrentSize);
Energy += -CurrentSize * 0.001f;
}
}
}
}