Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity Game Development Scripting

You're reading from   Unity Game Development Scripting Write efficient C# scripts to create modular key game elements that are usable for any kind of Unity project

Arrow left icon
Product type Paperback
Published in Dec 2014
Publisher
ISBN-13 9781783553631
Length 202 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Kyle D'Aoust Kyle D'Aoust
Author Profile Icon Kyle D'Aoust
Kyle D'Aoust
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Interactive Input FREE CHAPTER 2. GUI Time 3. Expandable Item Classes 4. Inventory 5. Enemy and Friendly AIs 6. Keeping Score 7. Creating Save and Load Systems 8. Aural Integration 9. Game Settings 10. Putting It All Together Index

Creating the device detector

The first function we'll create is one that will detect whether we actually have a gamepad plugged in. Unity inherently gives us a way to make this very easy.

Adding the variables needed

First, we'll add variables that we'll use in the detection and identification functions. Add these to the top of your script, just after the class declaration:

bool isControllerConnected = false;
public string Controller = "";

This Boolean will be used in later functions to determine whether there is a gamepad connected. The string will be used to hold the name of the gamepad connected.

Creating the detection function

Our next step is to add the DetectController function. This will use the Boolean we created earlier and check whether there is a gamepad connected. Add the following code to your script:

void DetectController()
{
  try
  {
    if(Input.GetJoystickNames()[0] != null)
    {
      isControllerConnected = true;
      IdentifyController();
    }
  }
  catch
  {
    isControllerConnected = false;
  }
}

This function uses the GetJoystickNames function of the input, which gets and returns an array of strings, which consists of the names of the connected gamepads. We use this to set our Boolean to true or false; true meaning there's a device connected and false meaning that the game couldn't detect a device. The reason we use a try-catch expression is because if there is no gamepad connected Input.GetJoystickNames() will give you an IndexOutOfRangeException error.

Creating the identifier function

Our last step in creating the device detector will be to add the ability to identify the gamepad connected. Add this function to the script, just below the DetectController function:

void IdentifyController()
{
  Controller = Input.GetJoystickNames()[0];
}

As you can see, we are assigning the name of the gamepad connected to our Controller variable. To use this function, call it within the DetectController function, in the if statement, where we set isControllerConnected to true.

You have been reading a chapter from
Unity Game Development Scripting
Published in: Dec 2014
Publisher:
ISBN-13: 9781783553631
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $24.99/month. Cancel anytime