- Home /
How can i create/clone gameobjects and if the value of the number of objects changed to destroy first the old ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class InstantiateObjects : MonoBehaviour
{
public GameObject prefab;
public Terrain terrain;
public float yOffset = 0.5f;
public int objectsToInstantiate;
public bool parent = true;
private float terrainWidth;
private float terrainLength;
private float xTerrainPos;
private float zTerrainPos;
private int numberOfObjectsToCreate;
private GameObject objInstance;
public void Start()
{
//Get terrain size
terrainWidth = terrain.terrainData.size.x;
terrainLength = terrain.terrainData.size.z;
//Get terrain position
xTerrainPos = terrain.transform.position.x;
zTerrainPos = terrain.transform.position.z;
numberOfObjectsToCreate = objectsToInstantiate;
generateObjectOnTerrain();
}
private void Update()
{
if (numberOfObjectsToCreate != objectsToInstantiate)
{
for (int i = 0; i < objectsToInstantiate; i++)
{
Destroy(objInstance);
}
generateObjectOnTerrain();
}
}
public void generateObjectOnTerrain()
{
for (int i = 0; i < objectsToInstantiate; i++)
{
//Generate random x,z,y position on the terrain
float randX = UnityEngine.Random.Range(xTerrainPos, xTerrainPos + terrainWidth);
float randZ = UnityEngine.Random.Range(zTerrainPos, zTerrainPos + terrainLength);
float yVal = Terrain.activeTerrain.SampleHeight(new Vector3(randX, 0, randZ));
//Apply Offset if needed
yVal = yVal + yOffset;
//Generate the Prefab on the generated position
objInstance = Instantiate(prefab, new Vector3(randX, yVal, randZ), Quaternion.identity);
objInstance.name = "Teleportation Booth";
if (parent)
objInstance.transform.parent = this.transform;
}
}
}
I want to use enum to decide either to create new gameobjects or to clone existing gameobject.
I know that Instantiate is for clone ? Then how do i create new objects ?
But i got comment not to use Instantiate: "You should never use Instantiate to create objects, that could be replaced by the previous one, at first create pool controller, that will have pre-defined quantity of objects, and if will be needed more it will create but if not, it will only change properties of unused object, using this you will get rid of memory leak(in this problem"
What i want to do is to decide by enum if to create new or to clone existing. And second i want to make in the Update function that if i type a new value of number of objects to create/clone then make the change in the Update function by comparing the int variables for changes and by destroying all the old allready created objects but it's not working.
Your question is confusing, but it seems like you need two separate object lists. One list of PREFABS, and one list of the Instantiated objects from the prefabs. Simply creating a new object via GameObject go = new GameObject()
won't help obviously, because it is just an empty object.
From your code it appears that you are trying to dynamically change the objects in the scene, like trees, bushes, clutter, etc. Is this true?
Your answer
Follow this Question
Related Questions
How can i Instantiate on the terrain from left to right ? 0 Answers
Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers
How can i spawn new gameobjects to be inside the terrain area ? 2 Answers
How can i change vertex posiiton to push it back -1 on z ? 1 Answer