How do I set category for buttons and use it to check if player select press a button in the same category?


In the prototype of my casual game, player can select several options to adjust difficulty of the game. These options are divided into a few categories. For example:

Category 1: Race
Opt 1: Human + 10 difficulty
Opt 2: Orc + 30 difficulty
Opt 3: Elf + 5 difficulty
Opt 4: Dwarf + 15 difficulty

When player chooses an option, it adds up the Diff Gauge. But when player chooses another option in the same category, it subtracts the previous option and then adds the new one.

This is what I tried and failed:

  • I added a new script to buttons to store their category as string
  • I don’t know how to use that in OnClick() in a button
  • I have no idea how to go forward

I’m still quite new to programming. Thank you for any suggestions.

Yes, they’re like radio buttons. Player can select only one option in a category.

The thing is, if you use the Toggle Group, you won’t catch in the code the way you should.

I’d do an enum with a property, like :

public enum RACE { Human, Orc, Elf, Dwarf }; // enum to define category
public delegate void RaceChange(RACE race); // event signature delegate
public event RaceChange RaceChanged; // event

private RACE _race; // variable
public RACE Race // accessor
	get { return _race; }
		if (value != _race) // if change (prevents triggering the mechanics all the time if you change it in Update for example)
			_race = value; // update value

			if (RaceChanged != null) // trigger the registered callbacks if any