Book Image

Unity Game Development Scripting

By : Kyle D'Aoust
Book Image

Unity Game Development Scripting

By: Kyle D'Aoust

Overview of this book

Table of Contents (17 chapters)
Unity Game Development Scripting
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Setting the quick-select items


In many games, there is a mechanism known as quick-items or quick-select items. These are items that the player uses often and wants to have access to very quickly without having to stop the game to go into their inventory. They are typically accessed by the number keys on the keyboard or the directional pad on a controller. We will add a small function into our inventory that will allow us to assign quick-items.

Setting the quick-select items quickly

Add this function to your script, under the RemoveFromInventory function:

void SetQuickItem(GameObject NewItem, int QuickInput)
{
  if(QuickItems[QuickInput].name != NewItem.name)
      if(QuickInput < QuickItems.Length)
        QuickItems[QuickInput] = NewItem;
}

This function takes in two variables, the GameObject that we want as the quick-item and the slot that we want to assign it to. In the actual function, we check whether the name of the GameObject in the current QuickItems array is the same as the name...