- Home /
Problem with substracting life to multiple gameObjects (c#)
I'm developping a 2D pixel based space shooter for mobile platform and i'm struggling with a problem. I'm trying to create a rocket with a area of destruction. I'm trying to substract an amount of life to each gameObjects in a specific circle. Everything's fine until i try to apply the damages. Every enemy has a "healthscript" and a "shotdetection" script. I try to do it through a foreach, but the life always decrease on the "healthscript" of the aimed gameobject. Here are my code (i found it through different only tutorial for some part because it's my first project):
HealthScript :
using UnityEngine;
/// <summary>
/// Gestion des points de vie et des dégâts
/// </summary>
public class HealthScript : MonoBehaviour
{
/// <summary>
/// Points de vies
/// </summary>
public int hp = 1;
private int damage;
/// <summary>
/// Ennemi ou joueur ?
/// </summary>
public bool isEnemy = true;
public void ApplyDamage() {
damage = gameObject.GetComponent<ShotDetection> ().damage;
hp -= damage;
// Destruction du projectile
// On détruit toujours le gameObject associé
// sinon c'est le script qui serait détruit avec ""this""
if (hp <= 0)
{
SpecialEffectsHelper.Instance.Explosion(transform.position); // Destruction !
if (gameObject.name == "Player") {
gameObject.SetActive (false);
}
else {
Destroy(gameObject);
}
}
}
}
ShotDetection :
using UnityEngine;
using System.Collections;
public class ShotDetection : MonoBehaviour {
//variables du type de shot
private GameObject Shot;
private GameObject DualShot;
private GameObject Rocket;
private GameObject DualRocket;
//variable explosion rocket
private float radius = 10;
private Collider2D[] objectsInRange;
/// <summary>
/// Ennemi ou joueur ?
/// </summary>
public int damage;
void Start() {
GameObject go = GameObject.Find ("Player");
Shot = go.GetComponent<PlayerScript> ().OneShot;
DualShot = go.GetComponent<PlayerScript> ().DualShot;
Rocket = go.GetComponent<PlayerScript> ().OneRocket;
DualRocket = go.GetComponent<PlayerScript> ().DualRocket;
}
void OnTriggerEnter2D(Collider2D collider)
{
// Est-ce un tir ?
ShotScript shot = collider.gameObject.GetComponent<ShotScript> ();
bool isEnemy = gameObject.GetComponent<HealthScript> ().isEnemy;
if (shot != null) {
if (Shot.activeSelf || DualShot.activeSelf) {
// Tir allié
if (shot.isEnemyShot != isEnemy) {
GetComponent<HealthScript>().ApplyDamage();
}
} else if (Rocket.activeSelf || DualRocket.activeSelf) {
if (shot.isEnemyShot != isEnemy) {
Vector2 location = collider.transform.position;
objectsInRange = Physics2D.OverlapCircleAll (location, radius);
foreach (Collider2D col in objectsInRange) {
EnemyScript enemy = col.GetComponent<EnemyScript> ();
if (enemy != null) { // linear falloff of effect float proximity = (location - enemy.transform.position).magnitude; float effect = 1 - (proximity / radius); enemy.ApplyDamage(damage * effect);
if (shot.gameObject != null) {
Destroy(shot.gameObject);
}
damage = shot.damage;
GetComponent<HealthScript>().ApplyDamage();
}
}
}
}
}
}
}
Sorry for bad english, thank in advance
i tried replacing line 52 in shotdetection by col.GetComponent().ApplyDamage(); but it didn't seem to work either.
Using the debug mode, i saw that damage value always was reset to 0. I tried to move it in different place but it didn't work neither. only the shot and the collided gameobject were destroyed.
Answer by WhiteDevil197 · May 21, 2015 at 09:35 AM
By debugging myself, i resolved the first problem by replacing line 52 by col.GetComponent().ApplyDamage(); Now i face a second problem. The "damage" value always was reset to 0. I tried moving it out of the foreach, but it didn't worked. only the shot and the collided gameobject get destroyed.
EDIT : the problem is in the line 22 of healthscript. The code should search in the collided gameObject and not in the gameobject with his life being substracted. That's why it work only on the collided gameobject. He's the only one to have "ShotDetection" with the right value of damage. Could someone tell me how i should do that pls?
Answer by fafase · May 21, 2015 at 08:19 AM
My two cents, use Physics2D.OverlapCircleAll.
Then you get an array of Collider2D that are inside the circle.
foreach(Collider2D collider in arrayCollider){
HelathScript hs = collider.gameObject.GetComponent<HealthScript>();
if(hs != null){
hs.ApplyDamage(damage);
}
}
You would be better off passing how much damage you want to apply instead of going backward trying to access back the script as you do to get the damage value.
Once you get that working, you can get to the next step by calculating the distance between the center of the explosion and the current object.
float distance = Vector3.Distance(centerPointcollider.gameObject.transform.position);
float damage = 1 / Mathf.Sqr(distance) * initialDamage;
Your answer
Follow this Question
Related Questions
Health Code, Damage and Healing 0 Answers
unity health bar not losing health 1 Answer
Implementing Damage to Gun, best way to implement health and damage. 1 Answer
How to make heart health system? 3 Answers
Enemies sharing health 2 Answers