Search Unity

Instantiate prefab of slider spawns function-less slider

Discussion in 'Scripting' started by RavenMikal, Jan 7, 2016.

  1. RavenMikal

    RavenMikal

    Joined:
    Oct 18, 2014
    Posts:
    144
    Code (csharp):
    1.  
    2. public Slider sliderprefab;
    3. public Slider slider;
    4. bool changed;
    5.  
    6. void Start() {
    7. slider = GameObject.Find("SliderPrefab").GetComponent<Slider>();
    8. changed = false;
    9. }
    10. void Update(){
    11. if (changed) {
    12.             if (slider != null) {
    13.                 Destroy (slider);
    14.             }
    15.             changed = false;
    16.         }
    17.         if (slider == null) {
    18.             slider = Instantiate(sliderprefab);
    19.             errorOut.text = "this happened";
    20.         }
    21. }
    22.  
    It spawns the slider, and just where I want it, but it doesn't slide. I have the inspector open at runtime, and when it instantiates, Most of the objects show blank transforms.

    What in the hell?! lol any suggestions, comments, anything, as always, would be greatly appreciated...
     
  2. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Well there could be a few things going wrong potentially, but the most obvious case is that any Unity UI object must be attached to the canvas to work. After instantiating you need to do slider.transform.SetParent(CanvasObject, false);
     
    JoeStrout likes this.
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Also, you need to have an EventSystem in your scene... without one, stuff will draw but not function.
     
    Nigey likes this.
  4. RavenMikal

    RavenMikal

    Joined:
    Oct 18, 2014
    Posts:
    144
    creating in a child in the canvas, and there is an EventSystem (have the inspector open when I run it).

    in the meantime I've bypassed this by re adding the slider component and reattaching it to its fill area and handle, but still trying to figure out where I'm going wrong.

    Thank you, though, both for the reply, some input is always better then none ^_^
     
    Nigey likes this.
  5. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    This is the usual checklist:
    • Is any other UI Element blocking my slider/button?

    • Does any parent element have a canvas group with interactable false?

    • Are there any hidden (Alpha 0) Elements blocking my slider/button?

    • Is there an Event System in the Scene?

    • Does the button work in a newly created Canvas?
    Does it work when you just manually drag and drop it in at runtime? Or does it work in a new scene with a fresh canvas?
     
    RavenMikal likes this.
  6. RavenMikal

    RavenMikal

    Joined:
    Oct 18, 2014
    Posts:
    144
    Okay, later then hell, but the resolution - upgrade to a more stable release...lol.
    Went with 5.2.3, and all has been well with several UI bugs I was having.
    Yay! Unity, you fixed it! :p

    Nigey, funny you should mention about that Alpha element, that did end up being the problem with another UI issue I was having where certain checkboxs weren't responding, ended up being the label, even though I thought I was clear of the text element, I just had to put it higher on the scene. >_< lol...
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Another (often easier) solution in this case is to just uncheck the "Raycast Target" checkbox for whatever is on top. Then it won't block clicks to things underneath. I often do this for debug overlays, which I do want to appear on top, but which I don't want to block my use of other UI.
     
    Nigey and RavenMikal like this.