Hello again,
I’m having a problem with instantating in my runner game. I’m using an empty gameobject as the game controller that spawns enemies. The goal is that every 3 seconds, a random number between 0 and 1 will be generated, if it’s 0 it will spawn one enemy type and if it is 1, it will spawn the other. It worked before in the previous function I posted, but when I moved it to the gameobject I’m now getting the error : Object reference not set to an instance of an object
public var spawnL :GameObject;
public var spawnF :GameObject;
public var lSpawnLoc: GameObject;
public var fSpawnLoc: GameObject;
private var enemyInstance: GameObject;
function Start (){
InvokeRepeating("Spawn",3.0,3.0);
}
function Spawn()
{
var choice: int = Random.Range(0,1);
if (choice == 0)
{
enemyInstance = Instantiate(spawnL,lSpawnLoc.position,lSpawnLoc.rotation);
}
else
{
enemyInstance = Instantiate(spawnF,fSpawnLoc.position,fSpawnLoc.rotation);
}
}
Spawn L and F are the enemy prefabs and the SpawnLocs are the spawn location within the game. I’m not sure where the problem is considering that it worked find when the instantiating code was set up for one specific enemy attached to the enemy spawn game object.
I have spawn L and F, and spawn loc f and F initalized on the inspector, enemyInstance is not on the inspector as I set it to private. Script is only attached to the specific game object.
public var spawnL :GameObject;
public var spawnF :GameObject;
public var lSpawnLoc: GameObject;
public var fSpawnLoc: GameObject;
InvokeRepeating(“Spawn”,3.0,3.0);
function Spawn()
{
var choice: int = Random.Range(0,2);
if (choice == 0)
{
Instantiate(spawnL,lSpawnLoc.position,lSpawnLoc.rotation);
}
else if(choice ==1)
{
Instantiate(spawnF,fSpawnLoc.position,fSpawnLoc.rotation);
}
}