Instantiate only one prefab at currentDate/onSliderChange

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 :slight_smile:

Hello @Grafikdude ,
Do you want your “Cube(x)” to only contain one prefab? If so, I think you could do this:

// ...
if (timeLine >= startDate+10){
    PlaceHolder = GameObject.Find("Cube (4)");
    if(PlaceHolder.transform.childCount == 0){
      GameObject obj = Instantiate(PrefabCube);
      obj.transform.SetParent(PlaceHolder.transform, false);
      currentLoadedCube = obj;
    }else{
      Debug.LogWarning("Prefab already created");
  }
// ...

I hope this helps. Sorry for my bad English, I’m from Germany.

I don’t know exactly what you’re trying to achieve here but i wrote some code that instantiates the correct amount of cubes you just need to adapt the GetPosition() function to calculate the correct position.
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);

        //Calculate the amount of cubes that need the be instantiated based on the fact that there instantiated every 10 years
        int amountOfCubes = currentDate / 10;

        for (int i = 0; i < amountOfCubes; i++)
        {
            //I don't understand what you where using the placeholder for?
            //So i would just calculate the position instead of moving the placeholder

            Vector3 position = GetPosition(timeLine);
            GameObject obj = Instantiate(PrefabCube, position, Quaternion.identity);
        }

    }

    public Vector3 GetPosition(float timeLine) 
    {
        //Calulate the position of the cube and return it
        Vector3 pos = Vector3.zero;
        return pos;
    }