- Home /
Making a previously selected UI button interactable again
Hey everyone, In my game I have a list (scroll view) filled with buttons. Each button represents a unit that you can pick for a battle. Once the unit has been selected, the unit's scriptable object is assigned to the corresponding slot, the mesh is instantiated and the button that was selected is set to be non interactable. Everything here works fine, however, when I re-open the menu for that slot, if I now want to replace that unit with a new one, the previous units button is still disabled, even after the slot has been successfully replaced. I have tried a "previous button" variable, however this was not successful. Keep in mind that each slot shares the same menu/list of fighters, selected buttons on other slots must be non interactable for any other slot also. I need a way to bring back interactability for a previously selected button when its slot has been replaced. Any help would be appreciated ! thank you :)
Answer by jstopyraIGG · Feb 12, 2019 at 11:37 PM
You need to reset the "intractable" state of the button. You can do it with code: https://docs.unity3d.com/ScriptReference/UI.Selectable-interactable.html. Easiest solution would be to:
-Have a class that has a list of "Button"s exposed to the inspector.
public List<Button> buttons = new List<Button>();
-Apply all your buttons to that list in the inspector.
-Change every single button's interactable state in OnEnable: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnEnable.html void OnEnable() { foreach(var button in buttons) { button.interactable = true; } }
This will reset the intractable state of all your buttons. Note this is not the most optimized solution, It would be best to only reset the button that needs resetting.
Yeah, I had a similar approach to this at first too :) So i know that I need to set the buttons to be interactable again, but my problem is knowing which one to make interactable. So if i select the first unit slot (say there are 4 slots) it brings up a list of units. I select the first unit (say there are 5) disabling the button, and also adding this unit to the slot. Now i go to slot 2, which is empty, and open the list. In the list, the button for the unit I selected in the previous slot should still be disabled, as it was selected to be in the other slot. So maybe for this slot I pick the second unit. So now the buttons for both unit one and two are disabled. And both slots are full. (up until here currently functions correctly in my script) So now imagine, I click on slot one again opening up the unit list (with both unit one and two's buttons being disabled) but I want to switch which unit I want in slot one. So i click on unit 3. This disables unit 3's button, however the original unit i selected for this slot (unit 1) still has its button disabled. I want to make this button enabled again, because it is no longer a selected unit so it should be free for other slots to utilize. Do you have any idea how i can do this ? :P
Ok, I understand now! I would create a class that you put on the slot button. In that class you hold a reference to the button of the unit you selected. Write a function in which you can set intractable state of current unit (button) to true, and set a new unit (button). This function gets called every time you assign a new unit to a slot.
public Button unitButton = null;
public void AssignUnit(Button newButton)
{
if(unitButton != null)
unitButton.interactable = true;
unitButton = newButton;
}
I would create a manager that has a currently selected slot, this is what you call the event on from the Unit OnClick event on the button.
SlotButton currentSlot = null;
//Called from the button OnClick() Event. (the slot button)
//newSlot is the button that is calling this function
public void NewSlot(SlotButton newSlot)
{
currentSlot = newSlot;
}
//Called from the button OnClick() Event (the unit button)
//newUnit is the actual button calling this event.
public void AssignUnit(Button newUnit)
{
currentSlot.AssignUnit(newUnit);
}
So now you have a manager that controls which slot is currently selected, and which unit is being applied. If a new unit is applied to a slot which currently has a unit, it will set that unit's button to intractable again.
Awesome ! Yeah that sounds like a good approach :) thank you so much !
Your answer
![](https://koobas.hobune.stream/wayback/20220612191622im_/https://answers.unity.com/themes/thub/images/avi.jpg)