Search Unity

How to turn on/off button visibility

Discussion in 'UGUI & TextMesh Pro' started by Velo222, Jan 10, 2015.

  1. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Hello,

    I have several buttons that I want located in one spot (the exact same spot), and I want to turn them off or on depending on what building the player selects. However, at first, I was simply turning on/off the button image via code. But I found out any button further underneath in Canvas order still doesn't get shown, because the button further up in the order is still blocking it.

    So, I was wondering how to turn off a button such that buttons underneath it can still be displayed and interacted with correctly. Right now, I can't access the "button" gameobjects that are children of the canvas. How would I do that too?

    If nothing else, how do I access the button "gameObject" via code (with new uGUI)?
     
  2. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Okay, nope still having problems.

    I can't assign buttons (i.e. gameobject buttons), under the canvas, as actual gameobjects in the inspector for some reason. This is using the new uGUI.

    There seems to be something always keeping me from using the buttons as game objects themselves. Not sure why.

    And if I set the button image to disabled AND the button script "interactable" variable as off, I still cannot have interactions with buttons located underneath them for some reason.

    Any help is appreciated.
     
    Last edited: Jan 11, 2015
  3. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    If you have a variable for something like the button image, you can get its gameObject and disable it like so:
    Code (csharp):
    1. buttonImage.gameObject.SetActive(false);
     
    andrec1250, etills, techbots and 2 others like this.
  4. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Thanks, makeshiftwings. Yes that is the code and I can finally grab the button gameobject through code now -- thanks for putting that out there too. But I still cannot assign the buttons to a prefab as a "GameObject" in the inspector. I'm not sure why.

    Is anyone else able to assign Canvas Buttons to prefabs in the inspector? One weird thing I noticed is when I tried to use the "Find" function in my code, it basically counted the button as a "Transform" instead of a "GameObject". So, I tried making a public "Transform" variable, but still was not able to assign the Button to my prefab even then.

    My biggest problem though, is that if you have a bunch of buttons "stacked" in one place on your GUI, there's no easy way to turn off the topmost button, and then have the button underneath it still interactable -- unless you disable the entire button. Which keeps scripts on those buttons from running anyways.

    I wish if maybe you made a button not be interactable and turned off it's image, then the button below it in transform order would be accessible.

    Right now, my only option is to disable the entire button altogether in order to make ones below it interactable.
     
  5. ensiferum888

    ensiferum888

    Joined:
    May 11, 2013
    Posts:
    317
    Is there a reason why you need to access the gameObject attached to the button rather than the button itself?

    In my code I'll just expose some public Buttons and use those directly:
    Code (CSharp):
    1. using UnityEngine.UI;
    2.  
    3. public class UITest : Monobehaviour{
    4.     public Button ForeMost;
    5.     public Button Second;
    6.     public Button Third;
    7.  
    8.     void OnEnable(){
    9.         ForeMost.gameObject.SetActive(true);
    10.         Second.gameObject.SetActive(false);
    11.         Third.gameObject.SetActive(false);
    12.     }
    13.  
    14.     private void disableAllButtons(){      
    15.         ForeMost.gameObject.SetActive(false);
    16.         Second.gameObject.SetActive(false);
    17.         Third.gameObject.SetActive(false);
    18.     }
    19.  
    20.     public void EnableButton(Button _toEnable){
    21.         disableAllButtons();
    22.         _toEnable.gameObject.SetActive(true);
    23.     }
    24.  
    25. }
    Also technically the button object should have a RectTransform instead of a Transform.

    You could also use a Button[] public variable and use for loops to enable/disable them.
     
    dev-4015 and slokAR-VR like this.
  6. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Hi ensiferum,

    Well the main reason I was having to access the overall GameObject itself was because everything else I tried wasn't working. And the only way I found to make a button be interactable correctly when stacked underneath another button on the uGUI, was to completely disable the entire button gameobject that was stacked on top of it (by stacked I mean placed in the exact same location on the GUI on the screen -- but higher in the hierarchy under the canvas).

    I could be missing something, but I tried disabling the image script on the button, disabling the button script on the button, disabling the image and button scripts on the button, and disabling the image script and the "interactable" variable on the button script (but leaving the button script enabled).

    Aside from that problem, in regards to assigning buttons in the inspector, I can't remember right now if I tried exposing a public variable as a "Button" type directly. I need to test that. But I did try it with the types "Rect Transform", "Transform", and "GameObject" so far, and none of those types let me assign a button gameobject to the inspector on a prefab.


    Sorry for the long book, but maybe there's something I'm missing. I know the whole system is relatively new, and I'm still learning it :)


    Edit: *Just tried making a public "Button" type variable on my script and assigning a button to it, and that didn't work either (on my prefab) :( Maybe I'm not setting something up right, I dunno.
     
    Last edited: Jan 12, 2015
  7. ensiferum888

    ensiferum888

    Joined:
    May 11, 2013
    Posts:
    317
    I think the only way to prevent the object from intercepting raycasts is to completely disable it like in the example above. Since the object is on the UI layer it will accept racasts as long as it's active (regardless of which components are on it)

    I might be wrong though what I've been doing now is simply disabling the gameObject itself when I don't need it.
     
  8. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437

    Well, I think you're correct. That's the only way, so far, that I've been able to make it work how I want it to. Turning off the button gameobject completely lets the buttons underneath it be clicked on......etc.

    I mean, I got a system working yesterday by which I can disable the button gameobjects, it just seems really "clunky" to have to it this way -- for lack of a better word. So, for now, I have to assign buttons by name to my variables, and since I'm making an RTS, this is going to be a LOT of extra code statements to do it this way. I could have around 60 - 100 buttons located in the same exact spot on my gui, that I'll have to assign individually through code by name.

    I'm willing to do it, but I was hoping for an easier more efficient way to both assign buttons to variables AND to expose buttons underneath other ones. I'm glad there's at least a way to do it though.