``Ok, I have an enemy spawning script here. How it works is there is a spawn controller object that tells each spawner object what round it is and other information that needs to be the same between all of the spawner objects. Here is the script on the spawn controller:
var Spawn : boolean = true;
var Round : int = 1;
var Enemies;
var AllDead : boolean;
var Spawners;
var EnemiesToSpawn : int;
var EnemiesSpawned : int;
var EnemiesLeft : int;
function Update()
{
Spawners = GameObject.FindGameObjectsWithTag("Spawner");
EnemiesLeft = Spawners.GetComponent(Spawner).EnemiesSpawned;
Enemies = GameObject.FindGameObjectsWithTag("Enemy");
if(Enemies.lenth == 0){
AllDead = true;
}
if(Round == 1)
{
EnemiesToSpawn = 20;
}
if(Round == 2)
{
EnemiesToSpawn = 30;
}
if(Round == 3)
{
EnemiesToSpawn = 40;
}
if(Round == 4)
{
EnemiesToSpawn = 50;
}
if(Round == 5)
{
EnemiesToSpawn = 60;
}
if(Round == 6)
{
EnemiesToSpawn = 70;
}
}
and here is the script on each individual spawner:
var Enemy : Transform;
var SpawnController : Transform;
var SpawnTrue : boolean;
var WaitTime : int;
var MinWait : int = 1;
var MaxWait : int = 10;
var Round : int;
var EnemiesSpawned : int;
var EnemiesLeft : int;
var EnemiesToSpawn : int;
var RoundDone : boolean = false;
function Start()
{
Round = SpawnController.GetComponent(SpawnTimer).Round;
SpawnTrue = SpawnController.GetComponent(SpawnTimer).Spawn;
WaitTime = Random.Range(MinWait,MaxWait);
}
function Spawn()
{
if(RoundDone){
SpawnTrue = false;
yield WaitForSeconds(15);
Instantiate(Enemy, transform.position, transform.rotation);
EnemiesSpawned ++;
EnemiesLeft --;
ResetWait();
SpawnTrue = true;
}
if(RoundDone == false)
{
SpawnTrue = false;
yield WaitForSeconds(WaitTime);
Instantiate(Enemy, transform.position, transform.rotation);
EnemiesSpawned ++;
EnemiesLeft --;
ResetWait();
SpawnTrue = true;
}
}
function Update()
{
EnemiesLeft = EnemiesToSpawn - EnemiesSpawned;
EnemiesToSpawn = SpawnController.GetComponent(SpawnTimer).EnemiesToSpawn;
var AllDead = SpawnController.GetComponent(SpawnTimer).AllDead;
Round = SpawnController.GetComponent(SpawnTimer).Round;
if(EnemiesSpawned == EnemiesToSpawn)
{
if(AllDead)
RoundDone = true;
print("Next Round");
EnemiesSpawned = 0;
EnemiesLeft = EnemiesToSpawn;
AddToRound();
}
if(EnemiesSpawned < EnemiesToSpawn)
{
RoundDone = false;
}
if(EnemiesSpawned > EnemiesToSpawn)
{
RoundDone = false;
}
if(SpawnTrue)
{
Spawn();
}
SetWait();
}
function ResetWait()
{
WaitTime = Random.Range(MinWait, MaxWait);
}
function SetWait()
{
if(Round == 1)
{
MinWait = 1;
MaxWait = 10;
}
if(Round == 2)
{
MinWait = 2;
MaxWait = 9;
}
}
function AddToRound(){
Round ++;
}
My problem here is in this line:
Spawners = GameObject.FindGameObjectsWithTag("Spawner");
EnemiesLeft = Spawners.GetComponent(Spawner).EnemiesSpawned;
I need to find the variable EnemiesSpawned from each of my spawners and add them together to get one number. How do you find a variable from each of the objects without having to list each one?
When I try that I get this error:
MissingMethodException: Method not found: 'UnityEngine.GameObject[].GetComponent'.
Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.ProduceExtensionDispatcher ()
Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.Create ()
Boo.Lang.Runtime.RuntimeServices.DoCreateMethodDispatcher (System.Object target, System.Type targetType, System.String name, System.Object[] args)
Boo.Lang.Runtime.RuntimeServices.CreateMethodDispatcher (System.Object target, System.String name, System.Object[] args)
Boo.Lang.Runtime.RuntimeServices+<Invoke>c__AnonStorey12.<>m__6 ()
Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.DispatcherKey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args)
SpawnTimer.Update () (at Assets/SpawnTimer.js:12)