How to make Among Us in Unity | Part 3 — The Keypad Task

Welcome to the series that will teach you how to create a game similar to Among Us, in Unity3D Engine.

Redefine Gamedev
4 min readOct 23, 2020

This is the third part of the series. For the first part and the full index follow this link.

Get the Game Kit and start building an Among Us-like game in minutes!

Among Us, in Unity
Among Us, in Unity

Also, you might want to check How to Make Among Us in Unity series on my channel, Redefine Gamedev. If you prefer the video version, I highly recommend you check that one as well.

In this episode we will look at how to make the keypad task work.

  • How to set up the assets needed
  • How to Generate an Unique Pin Code
  • How to Process Button Events
  • How to put everything together

This is the result we are looking for

An image displaying a key pad with 9 numbers. Is is going to be used for inputting the code
Keypad Task — final result

Assets Needed / Setting it up

We will only need the card as the other assets are fully made in Unity.

To display the buttons, we will create a new Game Object, parented to the Canvas. Don’t worry if you don’t have the canvas in the scene, you can create one from the top menu Game Object > UI > Canvas.

The object will be called KeypadTask and here is where we will set up everything needed for this.

We will create another child object, this time to the Keypad Task. Its name is Numbers and it will make use of a grid layout to display the buttons.

Image displaying Unity’s grid layout component with configuration options
Unity’s grid layout component

Next, we will proceed to create buttons — Game Object > UI > Button — and position them to be children of the Numbers game object. They will automatically be arranged.

We will also create two text fields, one for input and another for the initial code. These fields are not parented to the Numbers game object because we don’t want them to be in the grid layout.

Image depicting how to create a unity hierarchy for the buttons
Unity Hierarchy for Buttons

How to Generate an Unique Pin Code

Generating the code is a quite easy task.

string code = string.Empty;for (int i = 0; i < _codeLength; i++) {
code += Random.Range(1, 10);
}

Basically, we will go from 0 to the maximum code length and, for each value, we will append a random value to a string. In the end, the string will contain the generated code.

How to Process an Event from a Button

Clicking a UI button will generate an even that needs to:

  • add the button’s number to the current sequence
  • check if the newly created sequence matches the desired one
  • if not, and the lengths are equal or the inputted one is longer, fail
public void ButtonClick(int number) {
if (_isResetting) { return; }

// Add the new number to the existing code
_inputCode.text += number;
// Check if the code matches
if (_inputCode.text == _cardCode.text) {
_inputCode.text = "Correct";
StartCoroutine(ResetCode());
}
// Check if the code length is equals or longer
// The code did not match at this point
else if (_inputCode.text.Length >= _codeLength) {
_inputCode.text = "Failed";
StartCoroutine(ResetCode());
}
}

How to Put Everything Together

We need to add the KeypadTask as a component to the KeypadTask game object. After this, we will reference the script in each button to call the ButtonClick function.

Image depicting where each button’s onClick event should point to, that is the KeypadTask
Each button’s onClick action pointing to KeypadTask.Button Click

Don’t forget to add both of the input fields to the Keypad Task as well. They are needed to display the final code and the player’s input code.

Image depicting the label fields for the code and the input being assigned to the Keypad Task
Keypad task’s two label fields

Here is the code for the keypad task that we are using to make everything possible.

KeypadTask.cs

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class KeypadTask : MonoBehaviour { public Text _cardCode;
public Text _inputCode;
public int _codeLength = 5;
public float _codeResetTimeInSeconds = 0.5f;
private bool _isResetting = false;
private void OnEnable() {
string code = string.Empty;

for (int i = 0; i < _codeLength; i++) {
code += Random.Range(1, 10);
}

_cardCode.text = code;
_inputCode.text = string.Empty;
}

public void ButtonClick(int number) {
if (_isResetting) { return; }

_inputCode.text += number;

if (_inputCode.text == _cardCode.text) {
_inputCode.text = "Correct";
StartCoroutine(ResetCode());
}
else if (_inputCode.text.Length >= _codeLength) {
_inputCode.text = "Failed";
StartCoroutine(ResetCode());
}
}
private IEnumerator ResetCode() {
_isResetting = true;
yield return new WaitForSeconds(_codeResetTimeInSeconds);

_inputCode.text = string.Empty;
_isResetting = false;
}
}

Lastly, since now that we wrapped up the Keypad Task, we will create a prefab out of it. By using drag & drop, take the object from the hierarchy and move it to the project.

The Keypad Task will turn blue when it’s a prefab.

Want More?

You are covered! Head over to Youtube at Redefine Gamedev channel and check the video tutorial.

--

--