Hello,
I’m making a 2D local multiplayer platformer using c#. When a new scene is loading, I want to spawn some items and powerups on the map. I specified 6 transforms in my scene, Point1, Point2 etc. The game now features 4 different items, each of them is a prefab. When a scene is loading, at each transform/spawnpoint a random item should be instantiated. What is an easy way to do so?
you’ll need an instantiate for each location and then that instantiate will spawn a Random.Range array for each location. So you could have 1 method that’s called in your Start method of each level. That method will simple have 6 separate tranforms points call an instantiate of that array of items. gimme a bit i’ll post an example script
//you'll need 6 of these one for each location
public Transform spotOne;
//the [] mean array, in your editor you'll look for this gameObject and specify the length (how many, so 4 in your case and they'll be numbered 0 to 3)
public GameObject[] randomeLevelStartItems;
void Start()
{
//call the coroutine here at the start of each level, I am assuming each level is a new beginning, if not then you would call this coroutine wherever you've specified a new level starting
LevelStartItems();
}
private void LevelStartItems()
{
//so for any random range of an Int you need to go 1 more than the total number. a random range int does not include the highest number, you can't use a float for this since everything is a whole number
int randomItem = Random.Range(0, 4);
//you'll need to do this line of code for each location
Instantiate(randomLevelStartItems[randomItem], spotOne.position, spotOne.rotation);
//an alternative for a location would be and again for each location
Instantiate(randomLevelStartItems[randomItem], new Vector3 (x, y, z), quaternion.identity);
}