Quantcast
Viewing all articles
Browse latest Browse all 1488

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(); 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; }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>