hello.
So i wrote this script to spawn choppable trees from a prefab and finally got it working correctly. Now i am running into problems when trying to optimize the forest of only 300 trees. I checked static in the inspector and it did not help at all I am not saving anything while trying to batch them together. what else can i do to get these numbers down. I am currently getting only 3 to 6 fps when i look at a section of trees.
could it be the tree model that I am using to high of a poly count? 24m triangles and 50m verts are big numbers.
ya im getting millions that’s not a typo.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class RandomizeObj : MonoBehaviour {
public Transform obj;
public Vector3 postiion;
public GameObject objspawned;
public int totalobjs;
public float ObjectsetWidth;
public float ObjectsetLength;
public float respawntime = 3600.0f;
public float respawn = 3600.0f;
//respawn object if destryed after 60minutes - 3600 Seconds - 3600000.0f milliseconds
void Start () {
int spawned = 0;
while (spawned <= totalobjs) {
Vector3 position = new Vector3 (transform.position.x + Random.value * ObjectsetWidth, transform.position.y, transform.position.z + Random.value * ObjectsetLength);
RaycastHit hit = new RaycastHit ();
if (Physics.Raycast (position, Vector3.down, out hit)) {
Transform newObj = (Transform) Instantiate (obj, hit.point, Quaternion.identity);
newObj.transform.SetParent (transform);
newObj.name = gameObject.name + "_Obj" + spawned;
}
spawned++;
}
}
void Update() {
respawntime -= Time.deltaTime;
if (respawntime < 0) {
CheckForTrees ();
}
}
void CheckForTrees() {
int i = 0;
while (i < totalobjs) {
Vector3 position = new Vector3 (transform.position.x + Random.value * ObjectsetWidth, transform.position.y, transform.position.z + Random.value * ObjectsetLength);
objspawned = GameObject.Find(gameObject.name + "_Obj" + i);
if (objspawned == null) {
RaycastHit hit = new RaycastHit ();
if (Physics.Raycast (position, Vector3.down, out hit)) {
Transform newObj = (Transform)Instantiate (obj, hit.point, Quaternion.identity);
newObj.transform.SetParent (transform);
newObj.name = gameObject.name + "_Obj" + i;
Debug.Log ("Not Found Object at " + i);
}
}
i++;
}
respawntime = respawn;
}
}