So basically I wanna have powerups or rare items spawning in specific places in the map which I set like in subway surfers but I don’t really know how am I supposed to do it, so can anyone please help me?
I suggest that you have a script called something like “PowerUpSpawner”.
Inside that you have gameobject list like: public List<GameObject> prefabPowerUps;
and a Vector3 or Transform of the spawn location you want like public Transform spawnLocation;
Created your power up prefabs. Learn more about prefabs here: Unity - Manual: Creating Prefabs
Attach script to a gameobject and you should be able to add your prefabs to the list via drag and drop (or via code).
Inside the script have a spawn method with a random generator which uses the prefabPowerUps’ count to get the max index. With the spawn Location and the index of a random powerUp, you can instatiate (create) a new instance of that prefab object at the spawn location.
More info on Random number generator: Unity - Scripting API: Random.Range
More about Instantiate:
public class PowerUpSpawner
{
public List<GameObject> prefabPowerUps;
public Transform spawnLocation;
public void spawnPowerUp()
{
int numberOfPowerUps = prefabPowerUp.Count;
if (numberOfPowerUps > 0) // prevents index errors
{
int prefabPowerUpIndex = Random.Range(0, numberOfPowerUps); // Pick a number from 0 to (numberOfPowerUps - 1)
GameObject powerup = Instantiate(prefabPowerUp[ prefabPowerUpIndex ], spawnLocation.position, Quaternion.identity);
// IF you don't need to know what you spawned
// THEN you can exclude the 'GameObject power =' part
}
}
}