Unity Version Used:

  • Unity 2015.6.0f3 (works with current versions as well (2018 versions))


  • To toggle between options, and send the given object.
  • Used to toggle between a set of Items, or Weapons.
  • Used for toggle switching.
  • Puzzles


So I used this in NightBear to toggle between different weapons, and have the character equip a new weapon, based off the players selection. At first I thought, “This should be easy, toggling is already implemented in Unity”. It was to some degree, however, it didn’t give me back the weapon information. I still had to add a few extra steps that was still native to Unity, to get the information that I was looking for.

What I needed to do:

  • add a ToggleController, to Add New Toggles. Whenever a new Weapon was added.
  • Add a ToggleCheck, to return the toggle weapon information.
    • The data was also returned to the playerCharacter to tell it which weapon was selected by the player.
  • And a WeaponHighlight, to display the currently selected weapon to the player.

NOTE: There are several ways of doing this. I simply did what was an efficient solution, with the time constraint I had to get this working quickly.


  • Scripts:
    • WeaponToggleController.cs
    • WeaponElements.cs
  • Prefabs:
    • WeaponTogglePrefab
  • First we need our WeaponController GameObject within our UICanvas group.
  • Create an Empty GameObject, and assign it as a child of your UICanvas.
    • Add a GridLayoutGroup to your WeaponController object, and configure it to fit your needs.
      • Here are my settings:
      • GridLayoutGroup - WeaponToggle
  • Add WeaponToggleController as a Component to your WeaponController.
  • Then, add a Child UI GameObject
  • This Child UI GameObject will be your WeaponTogglePrefab.
    • Components for UI GameObject:
      • Toggle (group will be your WeaponToggleController)
      • WeaponElement
      • EventTrigger
    • WeaponTogglePrefab

Weapon Collision Process

To implement this, we need to understand how it will work.

In NightBear, I needed the PlayerCharacter(the bear) to collide with a weapon.


1. On Collision, identify if it is a weapon.

If it is a weapon, Check if the weapon is already in inventory.

If it is, reset the Ammo.

If it is not, add the weapon to the Weapon Inventory, and Notify the ToggleController of the new Weapon.

2. Feed the Toggle Controller the new weapon information.

Check if the Toggle doesn’t already exist.

3. Create a new Weapon Element. (this is based off the WeaponElementPrefab

Add the WeaponElement to the ToggleGroup.

Player – Select Weapon Process:

NB - WeaponSelections

After the new weaponElement has been added to the UI, the player has the freedom to select any weapon they like, and the bear needs to switch to the selected Weapon.


1. The player selects the weapon.

2. From the Weapon Element UI, and this will trigger the Event Trigger.


The Event Trigger is set to trigger WeaponToggleController.ToggleCheck(BaseEventData).

Since the BaseEventData contains this.gameObject, we can pull the WeaponElement information from this.gameObject.GetComponent().

Now if currentWeapon != selectedWeapon, we can continue to step 3.

3. We need to Notify the Player that a new Weapon has been selected. We can do that with a UnityEvent, and feed the information to a new weapon.

public class WeaponSwapEvent : UnityEvent<BaseWeapon> { }

In my case, I have a BaseWeapon variable that im passing through. However, all you really need to tell the PlayerWeaponHandler is:

  • Which weapon you want to use, or switch too. And that’s it.

4. This step is situational. In my case, I have an AutoWeapon Switch. When the Ammo == 0. Auto Switch to next available weapon. In which case, I need to tell the UI WeaponToggleController which weapon I switched too. Because of which, I have another event that pings back to the WeaponToggleController, using the same WeaponSwapEvent event handler.


Hope this helps with your projects!


Happy Developing!