Referencing a global variable

Hi guys,

trying to reference a global variable that i need to establish in a script and address in 2 seperate scripts -

I have a script that spawns enemies, and another that kills them. I need to allow the spawn script to only produce a certain number of enemies at any given time, that way I can restrict the amount in the game. This ive managed with the following -

var spawnBot : GameObject;
var spawnTime = 10.0;
var timer : float;
var botCount = 0;

function Start () {
timer = 0.0;
}

function Update () {
timer += Time.deltaTime;

if( (timer > spawnTime) (botCount < 2) ){
timer = 0.0;

Instantiate (spawnBot, transform.position , Quaternion.identity);

botCount++;

}
}

So this script currently allows only 5 bots. But! I need to establish this variable in a seperate script so i can address it in the script that kills bots - in order to allow it to spawn another if one is killed.

I’m assuming itll have something to do with using GetComponent(“botCount”), or something, but im not sure how to address the variable within a “botCount” script - which, also doesnt exist yet!

Any help much appreciated as ever,

Cheers

Will

Ok so far im assuming that the following script -

var spawnBot : GameObject;
var spawnTime = 10.0;
var timer : float;
var botCount = 0;

function Start () {
timer = 0.0;
}

function Update () {
timer += Time.deltaTime;

if( (timer > spawnTime) (botCount < 2) ){
timer = 0.0;

Instantiate (spawnBot, transform.position , Quaternion.identity);

botCount++;

botScript = GetComponent(“botCount”);
botScript.botAdd();

}
}

Will address a script called “botCount” and run the function “botAdd”, here is that script -

var botCount : float;

function Update () {

botCount = 0;

}

function botAdd(){

botCount += 1;

}

But in the early part of the first script above, I check whether I have less than 2 bots in the game at present. And currently I do this by using the variable “botCount” established at the start of the first script, but If i want to check it within the seperate “botCount” script, how can I do that? I understand how to run functions in other scripts… but not how to access the current count of a variable.

ps. know that botCount prob shouldnt be a float var, anyone suggest a better type?

sorry I’m new to this!

cheers

Will

You probably want to use a static variable:

static var botCount : int = 0;

Then your bot killer script can access it with the name of the script:

nameOfBotSpawningScript.botCount --;

However, static variables keep their value from session to session. So, to make sure it’s always 0 at the start, do this:

function Start () {
botCount = 0;
}

But there’s no need to do that for non-static variables; you can have:

var timer : float = 0.0;

at the top; no need to define it in a Start function. Also you can leave out the type…if you use a decimal it’s defined as a float. Without a decimal, it’s defined as an integer. But I leave the type in there anyway. (For example, if for some reason I ever change the value of “timer” to something else like 5 instead of 5.0, it will stay a float, so I don’t have to worry about remembering to put a decimal in there or not.)

–Eric

Cheers man,

thats exactly what I was after, just as I was reading about the use of “static” as globals, sorry, i’m used to Director terms, should really do my reading first. Thanks for that

Will