Hi all together!
I am having trouble with controling some UI elements.
I want to give all enemies some health and shield bars above their heads.
So I gave each one of them their own Canvas (in a prefab) on which they will display their bars (as sliders).
The hierarchy then looks like this:
-enemy
I am now having trouble with accessing the slider to change its maxvalue and current value.
This is my code for it (C#)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class StatController : MonoBehaviour
{
public Canvas HUDCanvas;
private Slider health_slider; //UI.Slider element
private text health_slider_text; //UI.Text element
void Awake()
{
health_slider=(Slider)HUDCanvas.transform.Find("HealthUI/slider");
}
}
The error then is:
Assets/scripts/StatController.cs(40,31): error CS0030: Cannot convert type UnityEngine.Transform' to
UnityEngine.UI.Slider’
I understand what that means but I do not know how to solve it…
Thanks for helping out!
James Butler
health_slider= HUDCanvas.transform.Find("HealthUI/slider").GetComponent<Slider>();
with HUDCanvas.transform.Find(“HealthUI/slider”) you are getting the transform “slider”. which you cant convert (Slider) into antoher script.
instead, when you get the transform you can make GetComponent and take the Slider Script from your gameobject/transform named “slider”
Well I was about to post but @martinmr beat me to it! But like he said when getting a game object you gotta get the component inside the game object. Kinda like saying check the GPU in your computer but you just look outside of the computer. You need to access inside and “Specify” what your looking for to use or check its contents.
PS: you should use code tags when placing code in a post it’s beside the undo button.
Thanks for the reply guys!
Amazing how you find help here!
I had it the way you told me to do it before but it didnt work either. Still doesnt…
health_slider=HUDCanvas.transform.Find("/HealthUI/slider").GetComponent<Slider>();
Now it sais:
NullReferenceException: Object reference not set to an instance of an object
StatController.Awake () (at Assets/scripts/StatController.cs:40)
ps: i added the code tags 
nevermind. spelling error 
thanks for the help guys.