The first script is just for creating new GameObjects:
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;
public bool randomScale = false;
public float setRandScaleXMin, setRandScaleXMax;
public float setTandScaleYMin, setTandScaleYMax;
public float setTandScaleZMin, setRandScaleZMax;
public bool generateNew;
private float terrainWidth;
private float terrainLength;
private float xTerrainPos;
private float zTerrainPos;
private int numberOfObjectsToCreate;
private GameObject objInstance;
private GameObject[] createdObjects;
private string objname;
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;
objname = prefab.name;
MyCustomEditor.TagsAndLayers.AddTag(objname);
generateNew = false;
generateObjectOnTerrain();
}
public void Update()
{
}
private void DestroyObjects(GameObject[] objects)
{
if (objects != null && objects.Length > 0)
{
for (int i = 0; i < objects.Length; i++)
{
DestroyImmediate(objects[i]);
}
objects = new GameObject[0];
}
}
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));
//Generate random x,y,z scale on the terrain
float randScaleX = Random.Range(setRandScaleXMin, setRandScaleXMax);
float randScaleY = Random.Range(setTandScaleYMin, setTandScaleYMax);
float randScaleZ = Random.Range(setTandScaleYMax, setRandScaleZMax);
//Apply Offset if needed
yVal = yVal + yOffset;
//Generate the Prefab on the generated position
objInstance = Instantiate(prefab, new Vector3(randX, yVal, randZ), Quaternion.identity);
if (randomScale == true)
objInstance.transform.localScale = new Vector3(randScaleX, randScaleY, randScaleZ);
if (parent)
objInstance.transform.parent = this.transform;
objInstance.tag = objname;
}
createdObjects = GameObject.FindGameObjectsWithTag(objname);
}
}
Then i have two other scripts.
The first one is attached to a UI button that is child of canvas in the hierarchy.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GenerateObjectsButton : MonoBehaviour
{
public static bool buttonClicked;
public void OnButton()
{
buttonClicked = true;
}
}
The third script is attached to toggle UI as child under canvas in the hierarchy:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ToggleValueChanged : MonoBehaviour {
private Toggle toggle;
Toggle.onValueChanged.AddListener((value) =>
{
MyListener(value);
});//Do this in Start() for example
public void MyListener(bool value)
{
if (value)
{
//do the stuff when the toggle is on
}
else
{
//do the stuff when the toggle is off
}
}
I have some problems:
- In the last third script in the ToggleValueChanged iām getting many errors on this part:
Toggle.onValueChanged.AddListener((value) =>
{
MyListener(value);
});//Do this in Start() for example
public void MyListener(bool value)
- What i want to do is when i click the button it will call the function generateObjectOnTerrain() from inside the Update function in the InstantiateObjects button. And according to if the toggle is checked or not decide if to destroy first the old gameobjects or not. If the toggle is checked(true) destroy if not just create more new.