- Home /
Question by
AlexTheHollow · Oct 03, 2015 at 10:58 AM ·
2dscript.spawnspawning-enemies
Spawn Random Enemies 2D
I'm trying to get enemies to spawn randomly. Any help with this?
using UnityEngine;
using System.Collections;
public class SpawnRandom : MonoBehaviour {
public GameObject[] objects;
public float spawnTime = 3f;
private Vector3 spawnPosition;
// Use this for initialization
void Start()
{
InvokeRepeating("Spawn", spawnTime, spawnTime);
}
void Spawn()
{
spawnPosition.x = Random.Range(-17, 17);
spawnPosition.y = 0.5f;
spawnPosition.z = Random.Range(-17, 17);
Instantiate(objects[UnityEngine.Random.Range(0, - 1)], spawnPosition, Quaternion.identity);
}
}
Comment
Answer by ado112 · Oct 03, 2015 at 12:58 PM
Last line is confusing:
Instantiate(objects[UnityEngine.Random.Range(0, - 1)], spawnPosition, Quaternion.identity);
Tryed this?
Instantiate(objects[UnityEngine.Random.Range(0,objects.Length - 1)], spawnPosition, Quaternion.identity);
Your answer
![](https://koobas.hobune.stream/wayback/20220612035839im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Make enemy damage the player,Enemy do damage to player Script 1 Answer
Multiple gameobjects but only one is screen wrapping. 0 Answers
Spawn waves & wait till it's killed, spawn again. 1 Answer
unity chose random gameobject and spawn it. 2 Answers
help with instantiating gameobject at random postition from a target gameobject? 1 Answer