- Home /
How can I acces the script of an object that I triggered?
Hello, I am working at a projectile script. The thing is that I want to be able to modify the health var from my enemy instance when the projectile triggers it. I want my projectile script to tell the enemyScript how much damage it should apply. So the real question is: "How on the earth can I acces only the script of that object? I have a lot of objects of the same type, so I need to acces the script of that triggered one. I will show you my scripts. Right now, my problem is that when I shot the first enemy that spawned it will kill only that instance and then if I continue shoting in the spawing order will works just fine. But if I shot the second spawned enemy, it will kill the first instance spawned. I am stuck, I need help!
Thank you very much!
Projectile.cs (Fragment)
public EnemyBasics enemyController;
void OnTriggerEnter2D(Collider2D col)
{
Debug.Log ("Collision");
if(enemyFire)
{
if(col.gameObject.tag == "Player")
{
PlayerController.OnDamageTaken(baseDamage);
collider2D.enabled = false;
}
}
else
{
if(col.gameObject.tag == "Enemy")
{
enemyController = col.gameObject.GetComponent<EnemyBasics>();
if(critChance > 0)
{
int random = Random.Range (0, 100);
if(random <= critChance)
{
outputDamage = baseDamage + bonusDamage + criticalDamage;
}
}
else
{
outputDamage = baseDamage + bonusDamage;
}
enemyController.OnDamageTaken(outputDamage);
}
}
}
EnemyBasics.cs
public void OnDamageTaken(float damage)
{
health -= damage;
}
GOD BLESS YOU! Thank you so much, my health was unfortunatly static.
I am glad so much! So please convert comment into the answer and accept it or rate;)
yeah, this is my first question here. Seems I cannot rate it yet.
We're sorry, but this cannot be voted up by you until you have at least 15 reputation.
But when I reach 15 rep I will rate it up. Thanks again!
Answer by Denvery · Jun 30, 2014 at 01:43 PM
Firstly, make sure that var "health" is not static. Also, please post fragments where you kill you enemy:)