I’m creating a race game that takes place on a highway with four lanes. After a set amount of time, a boost is supposed to spawn in one of the lanes, at random. I have created several games previously using XNA, but this is my first Unity experience, so I’m trying to adapt my methods correctly. What I tried was this:
I created a Main class out of habit, but I’m wondering whether it’s a good idea in Unity or not. In it I included the following code:
//Create boost list
List<Boost> boosts = new List<Boost>();
...
//Boost spawning function
public void spawnBoosts(){
spawnBoostsTimer += 1;
if (spawnBoostsTimer >= 60){
boosts.Add(new Boost());
spawnBoostsTimer = 0;
}
}
This is the constructor in the Boost class:
//Constructor
public Boost(){
laneNumber = Random.Range (1, 5);
if (laneNumber == 1){
transform.position = new Vector3((float)0.4, -1, 15);
}
else if (laneNumber == 2){
transform.position = new Vector3((float)1.5, -1, 15);
}
else if (laneNumber == 3){
transform.position = new Vector3((float)2.54, -1, 15);
}
else if (laneNumber == 4){
transform.position = new Vector3((float)3.6, -1, 15);
}
}
No new boosts are spawning, and I cannot find a way to make them do so. What’s the correct way of spawning instances of objects on random locations?
Thanks in advance!