I'm trying to use a 'spawnController' script to randomly instantiate gameObjects based on a predefined set of spawnpoints and destinations.
My 'SpawnSingle' script works, but I'm having difficulty creating the controller script.
Here is the 'SpawnController' below.
using UnityEngine;
using System.Collections;
public class SpawnController : MonoBehaviour {
public GameObject[] Enemies;
public Transform[] SpawnPoint;
public Transform[] PitStop;
public Transform[] Destination;
public int spawnCounter;
private Transform currentDestination;
private NavMove moveScript;
private GameObject newEnemy;
private SpawnSingle newSpawn;
// Use this for initialization
void Start()
{
// To-Do: Build a function to randomise the route and spawn at intervals
spawnCounter = 0;
Spawn();
}
void Spawn()
{
SpawnSingle.spawn.SpawnEnemy(Enemies[spawnCounter], SpawnPoint[spawnCounter], PitStop[spawnCounter], Destination[spawnCounter]);
// THIS DOESN'T work either?
// newSpawn.spawn.SpawnEnemy(Enemies[0], SpawnPoint[0], PitStop[0], Destination[0]);
}
}
The error is occuring in Spawn() above.
NullReferenceException: Object reference not set to an instance of an object SpawnController.Spawn () (at Assets/__Custom/Scripts/SpawnController.cs:29) SpawnController.Start () (at Assets/__Custom/Scripts/SpawnController.cs:22)
And for the 'SpawnSingle'
public class SpawnSingle : MonoBehaviour {
public static SpawnSingle spawn;
public GameObject Enemies;
public Transform SpawnPoint;
public Transform PitStop;
public Transform Destination;
private Transform currentDestination;
private NavMove moveScript;
private GameObject newEnemy;
public void SpawnEnemy(GameObject Enemies2, Transform SpawnPoint2, Transform PitStop2, Transform Destination2)
{
newEnemy = Instantiate(Enemies, SpawnPoint.position, Quaternion.identity) as GameObject;
moveScript = newEnemy.GetComponent<NavMove>();
currentDestination = PitStop;
moveScript.destination = currentDestination;
}
....
....
....
I've also googled it, and it's a common error... I think I'm missing something really basic here in terms of declaring or using a variable. Tried a few variations and other things that popped up in search but no luck.
I'm quite new at this, so thanks for the help.