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?
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;
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);
}
}
}
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).
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);
}
}
}
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:
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