Spawning random tiles
So I'm making kind of an endless runner esk game, and my plan is to have track pieces that spawn as you go. So far that's all working great, I have an endless track and thats perfect. Now I'm trying to figure out how to make it spawn a variety of track pieces, such as a straight one sometimes, other times a curve, etc.. My current code is
void Update () {
transform.position = new Vector2 (transform.position.x - player.speed, transform.position.y);
if (transform.position.x <= -100)
{
Destroy (gameObject);
}
if(transform.position.x <= 18 && spawned == false){
var track = (GameObject)Instantiate (
trackPrefab,
trackSpawn.position,
trackSpawn.rotation);
spawned = true;
}
}
I'm unsure as to how to make it spawn one of a few different options, any suggjestions? Also I'm aware that similar questions have been answered, but none of the answers have completly made sence to me, so I'm hoping to get a more direct answer to my specific problem.
Answer by poppysilvers1997 · Mar 21, 2018 at 01:01 PM
The thing I didn't understand was arrays. For those trying to find a solution who also don't know, you can store multiple objects or variables in one public variable by adding []. So it would look like
public GameObject[] trackPrefab;
//then to access it later, you can use
trackPrefab[number];
Then all you have to do is randomize the number and you have a pretty good random item selecter.
Your answer
Follow this Question
Related Questions
Having problem with instantiated prefabs 0 Answers
Instantiating prefabs, and making them move. (Projectiles) 0 Answers
Find instantiated prefab by name 3 Answers
Arrows fire in reverse. 1 Answer