Most likely simple (not less than zero)

Hello,

I have an EnemyCount script that I send hits to

 static var enemyCount : int = 0;

how do I make it so that the enemyCount can not be less than zero?

I have another script keeps track and it seems some of my hits are taking more than one because of the collisions

function Update () 
{
	print("enemy Count is" + enemyCount);
	
	if(enemyCount < 1)
	{
		//Do something	
	}
	if(enemyCount > 0)
	{
		//Do something else	
	}
}

Ive been searching for an answer but may just be looking in the wrong places. probably very simple but I just cant think of how to do it.

You have to only access it through functions if it needs to be within a range. If you try to control it through every script that might change it you’ll quickly find your code unmanageable.

static private var enemyCount : int; // or private static, I forget the order on that

static function EnemyCount() : int { return enemyCount; }

static function AdjustEnemyCount( amount : int ) {
  enemyCount += amount;
  if ( enemyCount < 0 ) enemyCount = 0;
}

Well a simple solution would be to say:

If(enemyCount < 0){
    enemyCount  = 0;
}

but I’m not sure if that’s a good solution in your situation, and I cant really tell what youre trying to do with it here… Also, why are some of your collisions registering multiple times?

Edit: ahh, yeah I think Vicenti hit the nail on the head.

SWEET!

Out of curiosity how would I access that type of function through another script? The way Ive been doing it is: EnemyCount.enemyCount += 1;
Id like to learn to do it the right way :slight_smile:

More like this:

var addEnemies : int = 0;

function OnTriggerEnter (enter :Collider)
{
	if(enter.gameObject.name == "Robot")
	{	
		print("Adding More Enemies");
		EnemyCount.enemyCount += addEnemies;
		Destroy(this);
	}	
}

Im just using the enemycount to open doors and give the players a achievement like so though:

if(enemyCount < 1  roomEntered == 4)
	{
		if(itemLeveUp01Dropped == false)
		{
			LevelUp01();	
		}
	}

Edit:
The first error I got was you cant access enemyCount through this level of high security and I was like wow thats some good stuff :slight_smile:

I think for now I’ll just go with Legend411’s solution until I get a little smarter or braver lol.

But thanks again my friend :smile: your alright!