Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheGalacticShaman · Feb 12, 2019 at 11:05 PM · uibuttonscripting beginner

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheGalacticShaman · Feb 12, 2019 at 11:47 PM 0
Share

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

avatar image jstopyraIGG TheGalacticShaman · Feb 13, 2019 at 12:13 AM 0
Share

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.

avatar image TheGalacticShaman jstopyraIGG · Feb 13, 2019 at 12:20 AM 0
Share

Awesome ! Yeah that sounds like a good approach :) thank you so much !

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

181 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a button appear on a specific state? 1 Answer

positioning ui buttons in a scrollview 1 Answer

Make a button selectable for controller but not interactable 0 Answers

Unity UI Button states 1 Answer

Buttons become invisible when changing color (on Button or on Image) in script -1 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges