Search Unity

acessing UI elements out of hierarchy

Discussion in 'Scripting' started by JamesButlerI, Oct 5, 2015.

  1. JamesButlerI

    JamesButlerI

    Joined:
    Oct 4, 2015
    Posts:
    8
    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
    - -Canvas
    - - -HealthUI
    - - - -slider
    - - - - -text
    - - some_other_stuff

    I am now having trouble with accessing the slider to change its maxvalue and current value.
    This is my code for it (C#)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class StatController : MonoBehaviour
    6. {
    7.   public Canvas HUDCanvas;
    8.   private Slider health_slider; //UI.Slider element
    9.   private text health_slider_text; //UI.Text element
    10.   void Awake()
    11.   {
    12.     health_slider=(Slider)HUDCanvas.transform.Find("HealthUI/slider");
    13.    }
    14. }
    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
     
    Last edited: Oct 6, 2015
  2. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    Code (CSharp):
    1. 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"
     
  3. LargerEagle

    LargerEagle

    Joined:
    Aug 15, 2012
    Posts:
    59
    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.
     
  4. JamesButlerI

    JamesButlerI

    Joined:
    Oct 4, 2015
    Posts:
    8
    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..

    Code (CSharp):
    1.  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 ;)
     
    Last edited: Oct 6, 2015
  5. JamesButlerI

    JamesButlerI

    Joined:
    Oct 4, 2015
    Posts:
    8
    nevermind. spelling error :p
    thanks for the help guys.