Home > Mobile >  spawning enemies in unity
spawning enemies in unity

Time:11-02

I want to spawn enemies from different and more than 50 positions.

I tried this code for spawning like 15 positions but what can I do for like 100 posiitons? any shortway for it? I tried to use s[randomIndex] but it doesnt defined so I got an error.

`

 switch (randomIndex)
            {
                case 0:
                    spawnedEnemy.transform.position = s0.position;
                    break;

                case 1: spawnedEnemy.transform.position = s1.position;
                    break;

                case 2:   spawnedEnemy.transform.position = s2.position;
                    break;

                case 3: spawnedEnemy.transform.position = s3.position;
                    break;

                case 4: spawnedEnemy.transform.position = s4.position;
                    break;

                case 5:
                    spawnedEnemy.transform.position = s5.position;
                    break;

                case 6:
                    spawnedEnemy.transform.position = s6.position;
                    break;

                case 7:
                    spawnedEnemy.transform.position = s7.position;
                    break;


                case 8:
                    spawnedEnemy.transform.position = s8.position;
                    break;

                case 9:
                    spawnedEnemy.transform.position = s9.position;
                    break;

                case 10:
                    spawnedEnemy.transform.position = s10.position;
                    break;

                case 11:
                    spawnedEnemy.transform.position = s11.position;
                    break;

                case 12:
                    spawnedEnemy.transform.position = s12.position;
                    break;

                case 13:
                    spawnedEnemy.transform.position = s13.position;
                    break;

                case 14:
                    spawnedEnemy.transform.position = s14.position;
                    break;

`

CodePudding user response:

I think you may be approaching this in a non Unity-centric way.

Let's think about what you're trying to do. It seems like you need a bunch of spawn points, and randomly select one of them. Then spawn an enemy at that location.

I would create a very simple MonoBehaviour that we use as basically just a tag. It's an empty Monobehaviour script called SpawnPoint.

Now, let's create a spawn point by creating an empty game object in the scene, and adding our new script to it. Make this a prefab so you can reuse it.

Duplicate it 100 times, and move them around in your scene to the correct locations.

Now that you have your spawn points, we need to gather them in a list, and choose a random one. The following is pseudocode but should be enough to get you started:

public class RandomSpawner() : Monobehaviour {

    List<SpawnPoint> _spawnPoints;

    void Awake() {

       // Gathers all spawn points in your scene. There are more optimized ways to do this. but this is a good start.
       _spawnPoints = FindObjectsOfType<SpawnPoint>();
    }

    public void Spawn() {

        // generate some random index from your list

        SpawnPoint randomSpawnPoint = _spawnPoints[index];

        GameObject enemy = Instantiate(enemyPrefab);
        enemy.transform.position = randomSpawnPoint.transform.position;

    }

    

}

Hope that helps you get started.

In my experience, if you're using a switch statement and there are more than about 4 options, it usually means you need to think about the problem in a different way.

  • Related