Hello! I am making a video game, where the player moves around the screen and presses the space bar to fire harpoons at fish. Each fish has a health bar, and I want to access the fill amount of the health bar when the harpoon touches the fish.
I have tried to access the health bar child of the fish, but I keep getting errors. What is the correct way to access a child (specifically an Image) of a parent GameObject. How do I change its fillAmount? Thanks in advance- George
Here is my code-
harpoonScript
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class harpoonScript : MonoBehaviour {
// Public variable
public int speed = 6;
private Rigidbody2D r2d;
public Text FishLabel;
public int fishKillCount = 0;
public Transform fishBar;
public Image fishBarFill;
// Function called once when the bullet is created
void Start () {
// Get the rigidbody component
r2d = GetComponent<Rigidbody2D>();
// Make the bullet move upward
float ySpeed = 0;
float xSpeed = -8;
Vector2 newVelocity = new Vector2(xSpeed, ySpeed);
r2d.velocity = newVelocity;
}
void OnTriggerEnter2D(Collider2D other) //hero hits side of enemy
{
fishBar = this.gameObject.transform.GetChild(3);
fishBarFill = fishBar;
if (fishBar.fillAmount > 0) {
fishBar.fillAmount = fishBar.fillAmount - 0.5;
}
else {
string fishStringfromText = FishLabel.text;
int fishIntfromText;
fishIntfromText = int.Parse(fishStringfromText);
++fishIntfromText;
FishLabel.text = fishIntfromText.ToString();
Debug.Log("GameObject found, reference is not null");
Destroy(other.gameObject.GetComponent<Collider2D>()); //Remove collider to avoid audio replaying
other.gameObject.GetComponent<Renderer>().enabled = false; //Make object invisible
Destroy(other.gameObject, 0.626f); //Destroy object when audio is done playing, destroying it before will cause the audio to stop
Destroy(GameObject.Find("harpoon_00001(Clone)"));
}
}
}
PS- Sorry if my question is too vague, or you don’t have enough information. Just tell me, and I can add more to my question 
2 Answers
2
Hello I think I spot some errors, lets go step by step.
- Make sure that the Image component you want to access has Image Type = Filled on the Inspector.
- You are getting a Transform component and saving that on an Image variable. (First error).
- Because fishBar is a Transform variable it doesn’t have a fillAmount parameter. (Second error).
Here’s the fixed code, make sure you have everything assigned on the inspector before running it.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class harpoonScript : MonoBehaviour {
// Public variable
public int speed = 6;
private Rigidbody2D r2d;
public Text FishLabel;
public int fishKillCount = 0;
public Transform fishBar;
public Image fishBarFill;
// Function called once when the bullet is created
void Start () {
// Get the rigidbody component
r2d = GetComponent<Rigidbody2D>();
// Make the bullet move upward
float ySpeed = 0;
float xSpeed = -8;
Vector2 newVelocity = new Vector2(xSpeed, ySpeed);
r2d.velocity = newVelocity;
}
void OnTriggerEnter2D(Collider2D other) //hero hits side of enemy
{
fishBarFill = this.gameObject.transform.GetChild(3).GetComponent<Image>();
if (fishBarFill.fillAmount > 0) {
fishBarFill.fillAmount = fishBarFill.fillAmount - 0.5;
}
else {
string fishStringfromText = FishLabel.text;
int fishIntfromText;
fishIntfromText = int.Parse(fishStringfromText);
++fishIntfromText;
FishLabel.text = fishIntfromText.ToString();
Debug.Log("GameObject found, reference is not null");
Destroy(other.gameObject.GetComponent<Collider2D>()); //Remove collider to avoid audio replaying
other.gameObject.GetComponent<Renderer>().enabled = false; //Make object invisible
Destroy(other.gameObject, 0.626f); //Destroy object when audio is done playing, destroying it before will cause the audio to stop
Destroy(GameObject.Find("harpoon_00001(Clone)"));
}
}
}
You didn’t post the error so I’m not sure what your problem is, but I would guess its due to several misunderstanding at line 33.
At line 33, you are trying to get the health bar, which is an image, by searching for a transform as if it were the child of the harpoon, not the fish.
With that said, to get the transform itself, try this:
void OnTriggerEnter2D(Collider2D other) {
fishBar = other.transform.GetChild(3);
etc.....
}
Now you should have a reference to the transform that has an Image component on it, but you still need to access the actual Image component before you can do anything.
void OnTriggerEnter2D(Collider2D other) {
fishBar = other.transform.GetChild(3);
fishBarFill = fishBar.gameObject.getComponent<Image>();
fishBarFill.fillAmount = 0.5f;
}
This general concept applies to pretty much everything in Unity. First, get a reference to the transform or gameObject that the component is on. Then get whatever component you need (transform, collider, material, renderer, script, text, etc).
@josehzz112- I inserted this new fixed code, but now the console gives me an error like this- > UnityException: Transform child out of bounds > harpoonScript.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Levels/Scripts/harpoonScript.cs:33) What does this error mean in my case, and how do I fix it? (Is it because 3 might not be the index of the child I want to access?)
– George-B