- Home /
How to Find Child Of A Child.? C#
Hello so I have been making a top down shooter. The problem that I am having is that when I spawn my enemy(It has a health bar on top of it) it works fine But when another enemy spawns and the first one is still alive the health bar system stops working. What I believe the problem is that when I choose to update the Bar I use an already chosen prefab. The problem with that is that when there are 2 of the same prefabs I believe that the system does not know which to update. Because of this I am trying to add a system in which the scrip itself finds the Object. I have the Script on the enemy and I need to find the Canvas called Enemy Health Then Find The "Actual Health Bar" Which is an image. By doing this I hope that it will Find its OWN health bar and not rely on an already chosen prefab. I need to know how I will be able to do in my script without using a set gameobject or prefab. Thank you for helping!
Enemy Health Bar Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class EnemyHealthBar : MonoBehaviour {
public GameObject HoldingEnemyHealth;
public Image HealthBar;
public Text ShowPercent;
public float health;
public float currenthealth;
// Use this for initialization
void Start () {
UpdateEnemyHealthBar();
}
public void UpdateEnemyHealthBar()
{
GetEnemyHealthManager();
float ratio = currenthealth / health;
HealthBar.fillAmount = ratio; //I DONT WANT TO USE HEALTH BAR BUT ACTUALLY FIND THE OBJECT FROM MY SCRIP HERE. THANK YOU!
ShowPercent.text = (ratio * 100).ToString("0") + "%";
}
public void GetEnemyHealthManager()
{
health = GetComponentInChildren<EnemyHealthManager>().health;
currenthealth = GetComponentInChildren<EnemyHealthManager>().currenthealth;
}
}
I can't quite understand what the issue is yet. I don't think we can diagnose your problem from the code sample you have linked. Can you briefly explain each component and it's location in the scene hierarchy?
I have some questions: - What is the purpose of the EnemyHealth$$anonymous$$anager? - Does the EnemyHealthBar exist as a child of an enemy? - Does each Enemy have it's own EnemyHealth$$anonymous$$anager?
All in all, this seems more complicated than it should be. I don't think you should have an "enemy health manager" at all. What is the purpose of that component?
Answer by Taylor-Libonati · Oct 16, 2017 at 11:08 PM
It looks like you have some confusion about how to use variables and classes. If you have a variable that is a reference to an object on your prefab then it will always just use that object.
Can you post a screenshot of your hierarchy when their are two enemies spawned?
I actually think the problem is with how you are grabbing the health. Really You never want to have two variables doing different things that you have to sync. Which is what you are doing with health right now. You are having the health bar search for the enemy every frame just to check its health. Instead of this, you should have a variable to the enemy that you can access whenever you want.
I suggest making a prefab for the enemy that has a game object for the enemy and a game object for the health bar. The health bar script will have a reference to enemy script and the enemy will keep track of its own health
So something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class EnemyHealthBar : MonoBehaviour {
public Enemy enemy; // This is the reference to your enemy
public GameObject HoldingEnemyHealth;
public Image HealthBar;
public Text ShowPercent;
//These arn't needed
//public float health;
//public float currenthealth;
// Use this for initialization
void Start () {
UpdateEnemyHealthBar();
}
public void UpdateEnemyHealthBar()
{
//Directly grabbing the variables from the enemy
float ratio = enemy.currenthealth / enemy.health;
//This should work fine as long as both enemies arn't referencing the same image
HealthBar.fillAmount = ratio;
ShowPercent.text = (ratio * 100).ToString("0") + "%";
}
}
Your answer
Follow this Question
Related Questions
How can I create a UI element and attach my script to it for a functional health bar? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Find out if gameObject is UI in 4.6 1 Answer
Options UI over all scenes 1 Answer