Question by
RealMcCoyGames · Mar 22, 2019 at 05:23 PM ·
instantiateunity 2did
How to instantiate a prefab with an ID?
Hi, I'm making an endless runner. In the background I want to have hills spawn in randomly.
So at the moment what I'm trying to do is:
Get a random number between 0 and 2
Instantiate a hill prefab based on the outcome of that random number.
I'm certain the problem is with the way I'm doing this ("hills_"+hillID) when instantiating. But I can't seem to find a way to create the prefab name to use in instantiate.
public GameObject hills_0;
public GameObject hills_1;
public GameObject hills_2;
private int hillID;
void Update () {
if (/*conditions*/) {
hillID = Random.Range(0, 2);
Instantiate(("hills_"+hillID), new Vector3(generationPoint.position.x, -2.64f, 0f), transform.rotation);
}
}
I also tried creating a string variable and using that but it didn't work either.
Would be great if one of you could point me in the right direction.
Thanks
Comment
Best Answer
Answer by RealMcCoyGames · Mar 23, 2019 at 05:43 PM
Managed to work around this, I use anima2d anyway so just set up the hills as one prefab with different animation states.
Your answer
![](https://koobas.hobune.stream/wayback/20220612194101im_/https://answers.unity.com/themes/thub/images/avi.jpg)