- Home /
How to reduce health on a children GameObject with my script?
Hello community.
How can I reduce health on a children GameObject when a parent GameObject can reduce it too?
The idea is that I can kill the children GameObject when parent GameObject is with health more than 0.
Here's a part of my script.
The variable "ps" is health.
void OnTriggerEnter2D(Collider2D collider)
{
//Es un disparo?
Disparar disparo = collider.gameObject.GetComponent<Disparar>();
if (disparo != null)
{
// Revisamos si es enemigo o compañero
if (disparo.esDisparoEnemigo != esEnemigo)
{
ps -= disparo.daño;
StartCoroutine(RendererRedColorDisparo());
// Destruimos el disparo
// No colocar solo Destroy()
//sino eliminara el Script
Destroy(disparo.gameObject);
if (ps <= 0)
{
// Muerto + EfectoEspecial de particulas
scoreScript.AddScore(scoreValue);
EfectosEspecialesScript.Instancia.ExplosionAsteroide(transform.position);
Destroy(gameObject);
}
}
}
}
Any help will be appreciated. Thanks.
Regards.
Answer by Mmmpies · Feb 02, 2015 at 08:01 PM
Hola, well that's most of my conversational Spanish used up!
You need a reference for the child object and it's own health value or put a script on the child and send the amount of damage to that.
There are lots of ways of doing this so how is your game going to work?
Probably as easy to do it all in one script, say you have 5 child game objects hold a reference for them and keep a variable for each ones health, unless they all lose value at the same time, in which case you only need one variable.
public GameObject Chico1;
public GameObject Chico2;
// 3 others
private float/int psChico1;
private float/int psChico2;
// 3 others
Then when your parent object gets hit just update those values just as you do the parent and destroy the child when it's psChico# goes 0 or below. It's a bit vague as I'm not sure how you want your game to work.
Answer by Ryujose · Feb 05, 2015 at 07:50 PM
using UnityEngine;
using System.Collections;
/// Comportamiento del disparo
public class Disparar : MonoBehaviour
{
/// Daño inflingido y tiempo en desaparecer
public int DesapareceEn = 1;
public int daño = 1;
/// El disparo es del jugador o del enemigo?
public bool esDisparoEnemigo = false;
void Start()
{
// Que desaparezca el objeto disparo despues de un tiempo definido
Destroy(gameObject, DesapareceEn); // Ver el int DesaparecEn mas arriba
}
}
This is my other script that I check if is enemy or not. I'm trying to do what you meant but it doesn't Works. Maybe is because i've that boolean and parent absorbes the hit?
I need an example, and try if it Works, i've a few days with it...
Thanks a lot for your help Mmmpies.
Edit: The thing that is more aproximate that I archieved to do is this, but I wanted to separate the collider detection and make it separate with parent and children, this can be done or is not possible?
void OnTriggerEnter2D(Collider2D collider)
{
//Es un disparo?
Disparar disparo = collider.gameObject.GetComponent<Disparar>();
if (disparo != null)
{
// Revisamos si es enemigo o compañero
if (disparo.esDisparoEnemigo != esEnemigo)
{
pschild -= disparo.daño;
StartCoroutine(RendererRedColorDisparo());
// Destruimos el disparo
// No colocar solo Destroy()
//sino eliminara el Script
Destroy(disparo.gameObject);
if (pschild == 0)
{
// Muerto + EfectoEspecial de particulas
scoreScript.AddScore(scoreValue);
EfectosEspecialesScript.Instancia.Explosion(transform.position);
EfectosDeSonido.Instancia.ReproducirSonidoExplosion();
Destroy(Torreta);
}
else
{
ps -= disparo.daño;
}
if (ps <= 0)
{
scoreScript.AddScore(scoreValue);
EfectosEspecialesScript.Instancia.ExplosionAsteroide(transform.position);
Destroy(AsteroideConTorreta);
}
}
}
}