Help with selecting multiple toggles but limiting selection to 3

The main challenges for me as a beginner programmer in Unity (C#) are 1. getting syntax right 2. knowing when to use certain “tools”. 3. lack of good documentation especially about writing clean efficient code (this is a warning for what you are about to see…).

I am trying to create a list of toggles from which the user can select at most 3 items at which point the rest of the toggles are disabled.

Note: From what I read, ToggleGroup is not appropriate for this since is limits toggle selection to 1, so everyone suggested writing code.

In pseudo code my code looks like:

Initialize Count variable
Public array of toggles (6)
Public toggle GOs (6, yes, the same ones as above, explanation later…)

Start

Set count to zero
Use Enable function to make sure all toggles are enabled (they should already be enabled, so this may just be fluff)

Update

Here I have listeners for all 6 toggles. Here is the syntax I am using:
ThisIsTheToggleGO.GetComponent().onValueChanged.AddListener((isOn) => { ActiveToggle(); });

ActiveToggle function

Goes through the array of toggles counting the ones which are selected

If the count is greater than 3 then it goes to the Disable function
else it goes to Enable function

Disable function (make toggle not interactable)
This goes through all the toggle gameobjects (one if statement for each toggle) and disables those that are not selected

Enable function (make toggle interactable)
This goes through all the toggle gameobjects (one line for each toggle) and enables the toggle


This almost works, except, the count is not a unique count, so when I select a single toggle, all the rest are disabled . If I put the listeners in Start, they don’t do anything although that is where the manual shows to put listeners. So because the listeners are in update the activetoggle count function is counting the same toggle every update. I then tried to set the count back to zero at both the disable/enable functions but then the count remained at perpetual zero.

I have several questions regarding this:

  1. My understanding is that the listener for a toggle is ‘listening’ for when the toggle is selected at which point I can have it take an action as a result of the select. Is this correct? If so, Where should the listener go if the end user is actively selecting and deselecting toggles. In Start, Update, it’s own function? Is there a better ‘tool’ choice than this? Like OnSelect? if so, why is it better? how the heck do I use it. the manual is not helpful with this. This is a which tool is best for the job question.

  2. Would it be possible to do all of this with a single Array? as opposed to using GameObjects in addition to the array. If so, can you point me in the direction in how to do this? I tried but wasn’t able to implement since I have extremely limited array experience - most lessons focus on using the GOs directly. Another which tool is best for the job question.

  3. How can I get a unique count of the toggles. This may be resolved by either removing the listeners from Update or just using GO’s and ditching the array, which I may do, but first… Is there any way to fix what I have already written to get a unique count?

I’ve looked online and was unable to find a good answer on how to implement this functionality so any help would be greatly appreciated.

Thanks in advance.

Privacy & Terms