Trying to get a child component of an Image (Making a floating healthbar)
I'm making a health bar on enemies (like in Destiny) but problem is I can not access the child of the health bar image that I instantiate on the canvas.
This is my health bar prefab I tried GetcomponentInChildren on Healthbar in my script and use .Fillamount but instead of getting Health Bar Value fill amount changed I get Health Bar Background fill amount, So anyone has idea how to access child of image within script? P.S. I also considered instantiate these two image separately, haven't tried it yet I think it would work, but It would be a lot more wise to do it this way.
Answer by Bir3D · Aug 31, 2019 at 03:02 PM
When using GetComponentInChildren, unity does a Breadth-first search, meaning you get the first image it encounters, in this case the background image.
There's several ways around this, here's 2:
Iterate through all children, using GetComponentsInChildren, store them in a collection(array/list), and access it by its index.
You can create a script on the instantiated object, that reference the image you need. Then all you need to do is call the script, and get the reference from there.
Your answer

Follow this Question
Related Questions
Slider Component Problem (Health Bar) 1 Answer
How do I create an undertale player health bar in C#? 1 Answer
Canvas problem 0 Answers
RigidBody2D not working right. Help! 0 Answers
enable and disable scripts using .enabled without hardcoding 3 Answers