Instantiate script help.

Hello, I am trying to use this script to spawn the enemies in my game (over a set interval of time using a random number, so that once every 15 seconds anywhere between 3 and 8 enemies will be instantiated)but it wont work, what is wrong? Thanks

public Transform[] ObjectsToSpawn;
public float SpawnInterval= 15;
public float MaxObjectsSpawned = 8;
public float MinObjectsSpawned = 3;

private float _NextSpawn

void Start() { _NextSpawn = Time.time + SpawnInterval;}
void Update()
{
if(Time.time >= _NextSpawn)
{
int objectsToSpawn = Random.Range(MinObjectsSpawned, MaxObjectsSpawned);
for(int i < 0; i < objectsToSpawn; ++i)
{
Instantiate(ObjectsToSpawn(Random.Range(0, ObjectsToSpawn.Length), transform.position. Quaternion.identity);
}
_NextSpawn = Time.time + SpawnInterval;
}
}

This script is written in C#, my guess is that you might have saved it as a javascript. Also, there are some major errors in the script; here's a couple things to keep in mind when using C# in the Unity engine:

  • You need to put `using UnityEngine;` and `using System.Collections;` at the beginning of your script.
  • You need to put the rest of your code inside `public class ScriptName : MonoBehaviour { }` ScriptName being the name of your script (e.g. ScriptName.cs) (this is important!)

Here is the working script:

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

    public Transform[] ObjectsToSpawn;
    public float SpawnInterval= 15;
    public int MaxObjectsSpawned = 8;
    public int MinObjectsSpawned = 3;
    public Vector3 minimum;
    public Vector3 maximum;
    public Vector3 center;

    private float _NextSpawn;

    void Start () { 
        _NextSpawn = Time.time + SpawnInterval;
        center = minimum - maximum;
    }

    void Update () {
        if(Time.time >= _NextSpawn) {
            int objectsToSpawn = Random.Range(MinObjectsSpawned, MaxObjectsSpawned);
            for (int i = 0; i < objectsToSpawn; i++) {
                //Instantiate a random object from the array ObjectsToSpawn within a minimum and maximum area which can be set in the inspector
                Instantiate(ObjectsToSpawn[Random.Range(0, ObjectsToSpawn.Length)], new Vector3(Random.Range(minimum.x,maximum.x), Random.Range(minimum.y,maximum.y), Random.Range(minimum.z,maximum.z)), Quaternion.LookRotation(center));
            }
            _NextSpawn = Time.time + SpawnInterval;
        }
    }

}