Skip to main content Link Search Menu Expand Document (external link)

Part 9: Changing Shops

Table of contents
  1. Challenge: Create a Shop Menu
  2. Enabling and Disabling a GameObject
  3. Using the SetActive Method
  4. Challenge: Switching Between Shops
  5. Challenge: Closing the Weapon / Armor Shops
  6. What’s Next?

Challenge: Create a Shop Menu

Next, you will add a Shop Menu that contains 2 buttons. One for your Armor Shop and one for your Weapon Shop.

  1. Create a new empty GameObject within your Canvas called Shop Buttons
  2. Add a Horizontal Layout Group component (Optional)
  3. Add two new Buttons to your Shop Buttons component
  4. Place your Shop Buttons on your screen in a way that looks good to you

When you’ve finished, your scene should look similar to this:

First Challenge Complete

Enabling and Disabling a GameObject

GameObjects can be enabled and disabled in a scene through the Inspector tab.

  1. Click your Armor Shop button in the Hierarchy
  2. Locate the check box next to the buttons name in the Inspector
  3. Check the box on and off.

Notice when you do, the GameObject name in the Hierarchy window will change to a light gray color and the button disappears from the Game and Scene tabs. When a GameObject is enabled it is said to be active. The video below demonstrates the Armor and Weapon shop buttons being enabled and disabled.

Using the SetActive Method

You can enable and disable a GameObject using a button by using its SetActive method. You will next modify the Armor Shop button to display the armor buttons hide the weapon buttons.

First, disable the armor buttons and weapon buttons.

  • Select the armor buttons parent GameObject in the Hierarchy
  • In the Inspector, disable the buttons
  • Repeat this process for your weapon buttons.
  • Select the Armor Shop button in the Hierarchy
  • In the Inspector find the button component
  • Add an Event Listener to the button’s OnClick Event
  • Add the armor buttons parent GameObject to the Event Listener
  1. Click the drop down to set the function to use when the button is clicked
  2. Select GameObject > SetActive (bool)

Select SetActive

  • Click the check box below the GameObject.SetActive function.

Note: If it is checked, it means that the registered GameObject will be set to active. If it is unchecked, it means that the registered GameObject will be set to be not active.

Activate Armor

  • Enter Play Mode and test your Armor Shop button

Challenge: Switching Between Shops

  • Update the Weapon Shop button to activate the weapon buttons.

When you’re finished, your scene should look similar to the one below:

Challenge: Closing the Weapon / Armor Shops

You can add multiple Event Listeners to a single event which can allow a single button to result in multiple actions.

  • Update your Armor Shop button to also deactivate your weapon buttons when it is clicked
  • Update your Weapon Shop button to also deactivate your armor buttons when it is clicked

When you’re finished, your scene should look similar to the one below:

What’s Next?

In Part 10: Adding Text, you will learn how to add in text labels that will eventually display information about your selected items!

Join the Discussion

Before commenting, you will need to authorize giscus. Alternatively, you can add a comment directly on the GitHub Discussion Board.