Random Spawn is NOT spawing Randomly,its always at SAME location ?

Random Spawn is NOT spawing Randomly,
it spawns always at SAME location ?
Why is this happening?

THIS IS A 2D GAME javascript
I am now getting this error and when hilited it marks the last code line around instantiate…

NullReferenceException: Object reference not set to an instance of an object
vid.Spawn () (at Assets/cook/Animations/vid.js:30

I have script attached and object name is correct im pretty sure this is an easy fix but I cant seem to find what

heres the entire code below

this is a 2d game

//var playerHealth : PlayerHealth;       // Reference to the player's heatlh.
var fly : GameObject;                // The enemy prefab to be spawned.
var spawnTime : float = 3f;            // How long between each spawn.
var spawnPoints : Transform[];         // An array of the spawn points this enemy can spawn from.


function Start ()
{
    // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
    InvokeRepeating ("Spawn", spawnTime, spawnTime);
}

//function OnCollisionEnter2D(coll : Collision2D) {
       
        // if (coll.gameObject.tag == "Enemy") {
         
function Spawn ()
{
     /*    
    //if(playerHealth.currentHealth <= 0f)
    
        // ... exit the function.
        return;
    }*/

    // Find a random index between zero and one less than the number of spawn points.
    var spawnPointIndex : int = Random.Range (0, spawnPoints.Length);

    // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    Instantiate (fly, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);

}

Removed as it was misinformation.

There are exactly two things that can be null in line 30:

  • Your fly reference
  • One of your spawnPoints references.

So make sure, at runtime, that your script has something assigned to the fly variable and that you don’t have empty / null elements in your spawnPoints array.

Keep in mind that references become null if the object that is referenced is destroyed. So make sure you check the references in the inspector at runtime. If one is null or has become null you have to find the cause of that.

You should Instantiate flies with an empty gameObject and each flies have a script with OnTriggerEnter that calls the empty gameObject method Spawn().

It seems that you don’t store your spawn points in spawnPoints array. This array is empty so it’s length is 0. Thus your spawnId is 0 and spawnPoints[0] is null.
You should first initialize this SpawnPoints array (in Start() probably), maybe like this :
spawnPoints = GameObject.FindGameObjectsWithTag(“SpawnLocations”);
This means you should give your SpawnPoints a tag.

PS : Or maybe you initialize this array in the inspector ?

I think the problem is you’re not understanding what that code is supposed to do. The word “random” might be confusing you. The idea of this code is, your map has spawn points that you placed. And it randomly picks one of them for each spawn.

The map has some cave openings, some goblin huts with doors, a road coming in from off-map … . Good spawn points. So, you drag a goblin to each spot, position and aim it, and turn that into the “spawn point” marker (by deleting it down to an empty, or just checking-off everything on it.) An empty Transform tells you the position and facing, which is perfect. Those carefully positioned empties are what go into spawnPoints.

Then, look at line 27. It randomly picks one place in the spawnPoint list. If it rolls a 2, it means to spawn at the exact spot where you put spawnPoint2 (in line 30.)

Again, I think the word “random” is confusing you. All it means is it rolls dice, and there are lots of ways to roll dice. If you want something to pop up just anywhere in a big area, different code can do that, which rolls dice in a different way.

look at your Vid script : the size of your array is only 1 !