Static vars and multiple enemies

I have an enemy script, which has the enemy health, ai and everything in one script, the script works like this, if life goes to 0 my object enemy die …But the problem with it is that whenever one enemy is deleted all of them are deleted. They all have same script attached to them and they all have same tag. I read that I should not use static vars and use GetComponent, but im noob and I can¡'t understand how to use that method, can hel me please?

Thx in advance!

Could you show some code?

If you have something like this:

static int Health;

then its understandable why all your enemys will die. A static field/property instance is shared between all Instances of the class which contains the static field/property. You dont want this, you want a separate variable and not a shared one. Just remove the static and be happy.

Only relevant if you have after removing the static keyword errors when accessing Health:
Find your component, cast the component to the type of your component (oh yeah, sounds badass, huh?) and access the health:

var enemy = myEnemyGameObject.GetComponent<Enemy>();
enemy.Health -= damage;

yes. i use:

static var Health = 100;

in the enemy script, and in other script use

ENEMYSCRIPT.Health -=10

to subtract health … all in JS …

Ok, great, then you can follow my last post as it is 100% relevant for your problem.

Have some probleams, look, in the enemy script i put:

var vida = 100;

And in the impact bullet script:

function Update()
{
	var dir = transform.TransformDirection(Vector3.forward);
	var hit : RaycastHit;
	
	Debug.DrawRay(transform.position, dir * 0.5, Color.red);
	
	if (Physics.Raycast(transform.position, dir, hit, 0.5))
	{
		if (hit.collider.gameObject.tag == "Enemy")
		{
			var enemy = myEnemyGameObject.GetComponent<Enemy>();
			enemy.Health -= 10;
                        Destroy(gameObject);
		}
	}
}

but doest work … have patience im noob :frowning:

If its not working, what exaclty is it doing? Errors, eg.?

Also, your health variable is called vida, but you substract 10 from Health, how should this work?

:yap but all call health, vida is in spanish :stuck_out_tongue: but all have same name … errors in line

var enemy = myEnemyGameObject.GetComponent<Enemy>();
var enemy = myEnemyGameObject.GetComponent<Enemy>();

This is wrong because you are using ();

It would need to be like this:

var enemy = myEnemyGameObject.GetComponent(Enemy);

Actually his method is fine, he’s using the generic GetComponent… the only problem is he forgot a period.

var enemy = myEnemyGameObject.GetComponent<Enemy>();

should be

var enemy = myEnemyGameObject.GetComponent.<Enemy>();

However, a separate issue entirely is that you don’t appear to be defining myEnemyGameObject anywhere; I think you took Marrrk’s example a bit too literally. What you probably want is:

if (hit.collider.gameObject.tag == "Enemy")
{
	var enemy = hit.collider.gameObject.GetComponent.<Enemy>();
	enemy.Health -= 10;
        Destroy(gameObject);
}

Assuming your enemy health/status script is called “Enemy.js”, and the public member variable in “Enemy.js” is named “Health” (not vida, Unity doesn’t intuitively translate Spanish, hehe).

yes. i put this:

var enemy = SCRIPRENEMYHERE.GetComponent(enemy);
enemy.health -= 10;

Error

BCE0005: Unknown identifier: 'enemy'.

haha yap is Health … but have this error:

BCE0138: ‘quack’ is not a generic definition.

This way it should work:

Enemy.js

var health : int = 100;

HitController.js

var myEnemyGameObject : GameObject;

// Use this for initialization
function Start () 
{
// You should define this variable first.
	myEnemyGameObject = GameObject.Find("Enemy");
}

function Update () 
{
	var dir = transform.TransformDirection(Vector3.forward);	
	var hit : RaycastHit;
	
	Debug.DrawRay(transform.position, dir * 0.5, Color.red);
	
	if (Physics.Raycast(transform.position, dir, hit, 0.5))
	{
		if (hit.collider.gameObject.tag == "Enemy")
		{
			// Check out the documentation for GetComponent method
			//var enemy = myEnemyGameObject.GetComponent<Enemy>();	// works in c# : function GetComponent (type : Type) : Component 
			var enemy = myEnemyGameObject.GetComponent(Enemy);		// works in js : function GetComponent (type : Type) : Component 
			enemy.Health -= 10;
            Destroy(gameObject);
		}
	}
}

and the link to doc : http://unity3d.com/support/documentation/ScriptReference/Component.GetComponent.html

Thx ahmetDP , but have a question, in the line:

myEnemyGameObject = GameObject.Find("Enemy");

have this error:

BCE0005: Unknown identifier: 'myEnemyGameObject'.

you can u explain me why?

Maybe I can, you copied the script wrong, scroll up inside the HitController.js script of ahmetD´s post and look at the bright and epic line which contains:

var myEnemyGameObject : GameObject;

ohh my fault … now can kill the enemies … but for example, i have two enemies, one in right corner and one in left corner, if i kill the right corner, die perfectly, if i kill the left corner, die perfectly … BUT if i kill the left enemy first, die the right corner enemy hahaha

You could remove the myEnemyGameObject stuff and replace the following line:

var enemy = myEnemyGameObject.GetComponent(Enemy);with:

var enemy = hit.collider.gameObject.GetComponent(Enemy);

finally its work!, thank you so much guys!

Yepp. That’s it.

“bright and epic line” :slight_smile: nice and kind saying. I liked it.