Creating a Difficulty System Toggle Switch

Dustin Cargile
5 min readApr 16, 2024

--

We are going to make a difficulty selection system using Toggles. What the heck is a Toggle though? Simply put a Toggle is a sort of Button that exists in one of two states: either true or false or, simpler still, on or off. It consists of several objects nested together. The Parent Object contains the Toggle Component. The Background and Checkmark are Images that show the state of the Toggle. Of course, the Label gives a concise description of the Toggle.

In an application, the Toggle will switch between On or Off states when clicked.

Each of the items that make the Toggle can be customized with custom Fonts and Images.

Now, with the explanations out of the way, we can make our difficulty selector. We start with one Toggle and apply any customization to it. Then, we will duplicate this Toggle, one for each difficulty we want to display.

We make sure to name these to avoid confusion.

Then space them out and edit the Label to better describe the Toggle, in this case, which difficulty the Toggle is associated with.

Now, we will Right-Click on the Panel and select Create Empty to create a new Empty Object. This will serve as our Toggle Group.

We then select all 3 Toggles and drag them onto the Toggle Group making them a Child of the Toggle Group.

Now, with the Toggle Group selected, we will click Add Component and search for Toggle Group. Simply click Toggle Group to add the Component to the Object.

On each Toggle, we will have to drag the ToggleGroup Object into the Group parameter located within each Toggle’s Toggle Component.

Immediately, when we run the application, we can see the Toggle Group in action. Each object is communicating with each other and will turn itself off if a different Toggle within the Group is turned on. This allows us to limit Player selections to only one, as such would be the case for difficulty selection.

Now, we can get into the coding portion of this. We can create a new Empty Parent for the Canvas and a new Empty Object under that Parent to act as our DifficultyManager.

We can attach a new Script to the Difficulty Manager. Within this Script, we will need to add the UnityEngine.UI and TMPro namespaces, giving us access to those Libraries.

We only need two Fields. One Field is an Array of our Toggles and the other will be our display text that we will have as a TextMeshPro Object.

Next, we will create the Methods. We will create a Method for each difficulty. Basically, these Methods will check if the Toggle within the Array at its specific location is turned on. If the Toggle is turned on, it will change the display text accordingly. In this case, we want the Easy Toggle to be at location 0 on the Array and if that Toggle is on, then we will display “Easy” on the display text.

Now, to assign these objects, we can select the DifficultyManager and click the Lock icon in the Top-Right of the Inspector. This will keep the DifficultyManager displayed in the Inspector even if we select a different Object.

Now, we will select all 3 Toggles and drag them onto the Toggle parameter in the Inspector. We can expand the Array within the Inspector to confirm the Toggles are in their preferred positions.

Lastly, after we add the display text Object, we can add it to the DifficultyManager as well.

Now, for each Toggle, we will click the plus sign on the On Value Changed section, drag the DifficultyManager into the GameObject Parameter, and find the Method associated with the Toggle within the DifficultyManager using the Function Selection Dropdown.

Another bit of code that we can add to the DifficultyManager is to run each of our custom Methods at Start so that our display text will be accurate from the get-go.

Now, we run the application. We can see that we can select each difficulty, only one can be selected at a time, and the display text displays the selected difficulty accurately.

Now, we have a difficulty selector that our Players can use in almost any game, except Elden Ring or anything made by From Software.

Thanks!

--

--