x


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;
}
}
more ▼

asked Nov 19 '10 at 02:35 AM

Tyler 2 gravatar image

Tyler 2
1.1k 211 246 264

@Tyler: If an answer helps you, then please click the check mark next to it so it will not show up on the list of unanswered questions. Thanks!

Nov 19 '10 at 07:01 AM PeterDC
(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

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;
        }
    }

}
more ▼

answered Nov 19 '10 at 03:16 AM

PeterDC gravatar image

PeterDC
277 2 4 14

Thank you. How would I make the range for the transform.position also random in the same style as the number of objects?

Nov 19 '10 at 07:03 AM Tyler 2

wait, this may sound weird, but what exactly do I attach your script to?

Nov 19 '10 at 07:15 AM Tyler 2

Updated my answer to show how to randomize the position.

Nov 19 '10 at 08:09 AM PeterDC

You could attach the script to anything in the scene you want, however, I recommend attaching it to a camera or empty.

Nov 19 '10 at 08:11 AM PeterDC
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3327
x1671
x572

asked: Nov 19 '10 at 02:35 AM

Seen: 911 times

Last Updated: Nov 19 '10 at 02:35 AM