Hey I need some advice on how I would go about spawning Eminem’s on play. For example when I press play and the game loads, I want thee to be 100 Eminem’s in play? What is the best way to do this?
First you should listen to the song "Spawn" by Eminem : Spoiler Little joke - i hope you can forgive me;-)! I think the best way would be to create prefabs for the enemies you want to spwan and then create the enemies over enemy = GameObject.Instantiate(prefab). Then you can set for example the rotation and position of the enemy via. enemy.transform.position = For random positions you can use the Random class.
You just made me laugh ! Haha I can forgive you. So realistically I could just place them in the scene if I wanted to? But each time I play I would want them to appear in a different place. Hmm ok, where would I add the script to each prefab ? I am pretty new to unity so learning. Thank you for your response.
You could create a GameObject called for example EnemyManager or EnemySpawner in your scene. Create a script (same name or different) for example EnemySpawner.cs and attach it to the object, In that you can spawn the enemies on start. Code (CSharp): public class EnemyManager : MonoBehaviour { public Transform enemyPrefab; public int enemyCount = 100; // Use this for initialization void Start() { for (int i = 0; i < enemyCount; i++) { SpawnEnemy(); } } private void SpawnEnemy() { var enemy = GameObject.Instantiate(enemyPrefab); enemy.transform.position = GetRandomEnemyPosition(); } private Vector3 GetRandomEnemyPosition() { //Only example var position = Random.insideUnitCircle * 10; position.y = 0f; return position; } It is up to you how you determine the positions. In the example the enemies spawn in a circle with radius 10. Hower they can still overlap each other. There are many ways to determine a position. You have to find one which suits your game best.
When you want to spawn the enemies on play you could create a public method SpwanEnemies and call this one when the button is pressed instead of doing it in OnStart. Also clearing the remaining enemies before creating new ones. Code (CSharp): public Enemy enemyPrefab; public int enemyCount = 100; public void SpawnEnemies() { ClearEnemies(); for (int i = 0; i < enemyCount; i++) { //Spwan enemies } } public void ClearEnemies() { var enemies = FindObjectsOfType<Enemy>(); foreach (var enemy in enemies) { Destroy(enemy.gameObject); } } When you get performance issues you should inform you about pooling the enemies(dont destroy the old ones but reuse them)