Hey, I basically have been banging my head getting error after error with a seemingly simple thing.
I have a enemyGenerator gameObject and script that has predefined spawnPoints : GameObjects[ ] in the inspector. I want it to basically randomly access the other spawn points, and call their createEnemy(). I was hoping this would be a way to keep track of each row of enemies in relation to their spawnPoint so I can move that row at once.
var SpawnPoints : GameObject[];
var EnemiesAllowed : int = 5;
var EnemiesTotal : int;
function Start () {
SpawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
nextSpawn = Time.time + 3.0;
nextMove = Time.time + 5.0;
EnemiesTotal = EnemiesAllowed;
}
function Update () {
Enemies = GameObject.FindGameObjectsWithTag("Enemy");
if ( (Enemies.Length < EnemiesAllowed) (Time.time > nextSpawn) )
{
var spawnCreateObj = SpawnPoints[(Random.Range(0, SpawnPoints.Length))];
var spawnComponent = spawnCreateObj.GetComponent("spawn").createEnemy();
EnemyID++;
nextSpawn = Time.time + spawnWaitTime;
}
I get “NullReferenceException: Object reference not set to an instance of an object”.
I have two questions, why can’t I call that function? And is this a correct way of going about it?
var spawnComponent : Component = spawnCreateObj.GetComponent(spawn);
spawnComponent.createEnemy();
Actually got me into the other gameObject’s script, but none of the variables defined in Start() are defined. Is there a way to get those variables initialized? Right now all the variables below are null.
function Start () {
//GameWorldObject is defined in the inspector as the Enemy Generator object with all it's variables below
if (GameWorldObject)
{
EnemyPrefab = GameWorldObject.GetComponent("GenerateEnemy").EnemyPrefab;
EnemyID = GameWorldObject.GetComponent("GenerateEnemy").EnemyID;
Player = GameWorldObject.GetComponent("GenerateEnemy").Player;
print("got player");
}
}
Those variables are inaccessible as you’re defining them within Start() - therefore, they’re only accessible within Start(). Move the stuff that you need to access from elsewhere out into the class scope.
You probably want to rewrite this script anyway using an InvokeRepeating() in Start() to call a function containing that spawning logic (i.e. what you currently have in Update()). You don’t want to be running FindGameObjectsWithTag in Update(). After implementing the InvokeRepeating version, you can delete the Update function completely.
EDIT:
Maybe something along the lines of the below. Haven’t done UnityScript for a bit so the syntax might need some massaging.
var SpawnPoints : GameObject[];
var EnemiesAllowed : int = 5;
var EnemiesTotal : int;
var SpawnInterval : int = 3;
function Start()
{
SpawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
EnemiesTotal = EnemiesAllowed;
InvokeRepeating("Spawner", SpawnInterval, SpawnInterval);
}
function Spawner()
{
Enemies = GameObject.FindGameObjectsWithTag("Enemy");
if (Enemies.Length < EnemiesAllowed)
{
var spawnManager = SpawnPoints[(Random.Range(0, SpawnPoints.Length))].GetComponent("spawn").createEnemy();
EnemyID++;
}
}