I have a project which I load two road planes one after another to create endless road.I also have a script attached to camera which creates tree’s.All of the code works perfectly fine the tree’s are perfectly created but I want to destroy them after they are out of the camera’s sight.I tried to destroy them by using destroy(gameobject) but I destroy myself.
So can you help me in this guys ?
My code:
void generateSideTress( )
{
GameObject treeObj = GameObject.Instantiate( sideTres[ Random.Range(0,sideTres.Length-1 ) ] ) as GameObject;
treeObj.transform.position = new Vector3( Random.Range(-80,-60 ) ,0 , playerObj.transform.position.z + 780 ) ;
treeObj.transform.Rotate (0, Random.Range (0, 36) * 10, 0);
}
My code to destroy them I thought that maybe if I destroy them after a second it may work but it doesnt. (not works)
Destroy (treeObj, 1);