I am currently hiding/disabling buttons to navigate through "screens" in a unity game, with code similar to this within each onclick:

public void settingsClick()
{        
    SettingsBtn.SetActive(false);
    SinglePlayerBtn.SetActive(false);
    MultiPlayerBtn.SetActive(false);
    SoundBtn.SetActive(true);
    DifficultyBtn.SetActive(true);
    BackBtn.SetActive(true);
}

The problem is this is horribly unscalable, for each button I add, I need to go and add in more enable/disables into each already existing button - what better options are there? I have considered making a function that sets it for each screen, and calling that (i.e. setSettingsView), but that only makes it slightly more scalable, in that I still need to go in and edit multiple functions each time I want to add a button

share|improve this question
    
One trick is that the Unity button's OnClick can have more than one method call assigned. You can directly access the buttons you want to activate/deactivate through calling gameObject.SetActive(bool) on each one you want to target without writing a new method. It would all be setting it up through the Editor--which also has some drawbacks for scalability. – Tartle Wizard Dec 15 '16 at 20:46
    
@Sys_Admin_Luddite so instead of doing it in code, I can do it in the editor - but I would still have to go back and retroactively edit each button that already exists whenever I want to add a new one? – user2813274 Dec 15 '16 at 20:51
    
yeah you would. It doesn't really solve your problem. I usually nest buttons in panels (a Canvas Image without a graphic). That way I can Activate/Deactivate a group of things all at once. It makes it a little easier. – Tartle Wizard Dec 15 '16 at 20:53
    
You could create a hierarchy of the buttons that hide show themselves. For example you can place your Sound Difficulty and Back button tho be children of the same object called Settings Panel or something and the operate the disable enable on that panel. Then if you need a new button in the settings you simply add it under the panel and the code does not change. – Uri Popov Dec 16 '16 at 8:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.