Creating a Difficulty Toggle in Unity 2021 URP

GameDev Dustin
3 min readSep 9, 2022

--

In this article we’ll set up a toggle group with individual toggles for different difficulty levels.

Initial Scene Setup
As shown above, we need a Canvas as always to get our UI rolling.

From there, we’ll add an Image background with a TMP Text child titled “Difficulty”.

Then create an empty game object and name it “DifficultyToggleGroup”.

Then add three UI Toggles as children of the empty game object and rename them different difficulty levels.

Adjust all canvas elements size and position until you are satisfied.

On the empty game object, add the “Toggle Group” component.

On each toggle element, drag the empty game object with the Toggle Group component attached onto the “Group” field.

Create a new script and assign it to the DifficultyToggleGroup empty game object.

The DifficultyToggles Class/Script
Add the above placeholder code to the newly created script.

We simply want an array to hold our toggle elements and an associated public method to check if each is true.

Back in the Unity Editor, we need to assign each toggle in the correct order to match our Debug.Log assumptions.

Now, for each toggle element, add a Unity Event to “On Value Changed (Boolean)” by dragging the empty game object with the script attached into the field as shown above.

Select the script and the method associated with each toggle from the dropdown.

Now, when we run our game and click on each toggle, we should see the appropriate debug.log message appear.

This confirms everything hooked up correctly and acts as a foundation for any implementation hereafter.

In the next article we’ll create another toggle system that swaps out canvas elements when changed.

--

--