Hello together,
using Unity 2020.3.13 / C# here on a simulation of a pyramide.
I’ve made a Slider from the year of 2013 up to 3083. Every 10 years a new Prefab “Cube” is getting instatiated. (Proberly, there is a better way to write a if-case for every 10 years - but it is working).
So far so good. The problem is that it will spawn more than one prefab on my “Cube-Anchor” everytime im changing my slider-value.
How can i regulate to only spawn one nested prefabCube (always the same prefab) on any diffrent anchor (“Cube (4)”, “Cube(5)”, …)?
Here’s the code:
public class InstantiateObject : MonoBehaviour {
public int startDate = 2013;
public int endDate = 3083;
private GameObject PlaceHolder;
public GameObject PrefabCube;
private GameObject currentLoadedCube;
public void onSliderChange(float timeLine)
{
int currentDate = (int) Mathf.Lerp(startDate, endDate, timeLine);
print(timeLine);
if (timeLine >= startDate+10)
{
PlaceHolder = GameObject.Find("Cube (4)");
GameObject obj = Instantiate(PrefabCube);
obj.transform.SetParent(PlaceHolder.transform, false);
currentLoadedCube = obj;
}
if (timeLine >= startDate+20)
{
PlaceHolder = GameObject.Find("Cube (5)");
GameObject obj = Instantiate(PrefabCube);
obj.transform.SetParent(PlaceHolder.transform, false);
currentLoadedCube = obj;
}
}
}
Here is a screenshot of the editor:
Thank you for your help ![]()
