Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ryujose · Feb 02, 2015 at 04:17 AM · gameobjectshootingchildrenontriggerenter2dhealth

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Recoil on self-object 1 Answer

Grabbing all Children and components of those children 1 Answer

Why do I get: "Cannot cast from source type to destination type"? 1 Answer

Game Object Looses Children When Return to Scene 1 Answer

Enemies sharing health 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges