Search Unity

Making Enemy's spawn in random spawnpoints

Discussion in 'Scripting' started by MrSanfrinsisco, Sep 24, 2018.

  1. MrSanfrinsisco

    MrSanfrinsisco

    Joined:
    Jan 17, 2018
    Posts:
    31
    I'm creating a small game and I'm trying to figure out how to make bombs spawn on spawnpoints. They will spawn above the screen and fall into the game view, the objective is that the player has to avoid getting hit from a falling bomb, if they get hit they lose health (The health part hasn't been implemented so all that it does is kill the player right now). I'm new to Unity and really haven't worked with Instantiating objects so I apologize if I'm way off. Here's my code:
    Code (CSharp):
    1.  
    2. public class BombManager : MonoBehaviour {
    3.     public Transform[] spawnPonts;
    4.     public GameObject[] enemyPrefabs;
    5.     public float respawnTime = 5f;
    6.  
    7.  void Start () {
    8.         InvokeRepeating("Spawn", 3f, respawnTime);
    9.  }
    10.     void Spawn() {
    11.         Instantiate(enemyPrefabs[0], Random.RandomRange(spawnPonts[0], spawnPonts.Length));
    12.     }
    13. }
    I'm getting an error at
    Code (CSharp):
    1. Random.RandomRange(spawnPoints[0], spawnPoints.Length));
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    First,
    Random.RandomRange
    is obsolete; use
    Random.Range
    instead.

    Next, this method expects either two int or two float values.
    What you have supplied is a Transform and an int.

    You probably just meant to do this:
    Code (CSharp):
    1. Random.Range(0, spawnPoints.Length));
     
    MrSanfrinsisco likes this.