Using Parameters and Class Communication to Check a Combo | AR Unity Developer

Derek Anderson
4 min readMay 14, 2024

--

With the Magic Box project planned out, we can begin putting the plan into code. We’ll start with the Box Manager and the Gem class.

I’m working with some assets for this Magic Box project that include a box, a set of gems, and a skull. The box will open to reveal another gem if three gems are placed in the correct order. The order of these gems can be found on the bottom of the skull. If the order is correct, the box will communicate with the box lid, and the animator on the lid will play to open the box and reveal the other gem inside. Start off by creating a new script that will be added to the box called BoxManager.

On the BoxManager script, create the necessary variables. Two string variables are needed, one for the correct sequence and another for the entered sequence. When a gem is tapped, the color of the gem will be added to the entered sequence. These two strings will be compared to each other, with the proper sequence talking to the animator and opening the box.

Two int variables will be needed for the current gem entered and the total amount of gems. They will be compared to each other, and once those are the same, the string variables will be compared to each other. If the sequence is the same, the box opens. If not, the game will reset.

The Animator variable will be public, and that can be filled in the Unity Editor with the animator on the lid of the magic box set.

    private string _correctGemOrder = "BlueRedGreen";
private string _enteredGemOrder = "";

private int _amountOfGems = 3;
private int _currentGem = 0;

public Animator _boxAnimator;

The first method of the BoxManager script will be GemSelect(). This will add the color of the gem to the entered gem order and increment the current gem number. When the current gem number matches the total number, it will compare to the correct gem order.

    public void GemSelect()
{
//add color of gem to _enteredGemOrder

//increment _currentGem
_currentGem += 1;

//if _currentGem == _amountOfGems, compare to _correctGemOrder()
if (_currentGem == 3)
{
CompareGemOrder();
}

}

void CompareGemOrder()
{

}

Currently there isn’t a color of the gem yet, which will be passed in as a parameter from the Gem script. Create a script called Gem and create a string variable for the gem color. The string is blank for now because each gem will have a different color.

public string _gemColor = "";

Back in the BoxManager script, GemSelect() can now pass in a Gem parameter. The color of the gem can now be added to the entered gem order, which will be compared to the correct gem order.

    private string _correctGemOrder = "BlueRedGreen";
private string _enteredGemOrder = "";

private int _amountOfGems = 3;
private int _currentGem = 0;

public Animator _boxAnimator;

public void GemSelect(Gem currentSelectedGem)
{
_enteredGemOrder += currentSelectedGem._gemColor;

_currentGem += 1;

if (_currentGem == _amountOfGems)
{
CompareGemOrder();
}

print("Gem selected");
}

CompareGemOrder() will compare the string variables toget.her, opening the box up if they match and resetting the game if they don’t. This will be done with an if statement.

    void CompareGemOrder()
{
if (_correctGemOrder == _enteredGemOrder)
{
print("Order is correct");
OpenBox();
}
else
{
print("Order is incorrect");
ResetGame();
}
}

OpenBox() does exactly what it says. This method will cause the box lid to open by dealing with the animator on the box. The animator controller shows that transition uses the trigger Open to cause the box lid to go from closed to open. OpenBox() accomplishes that by talking to the animator and setting the trigger in dot notation.

    public void OpenBox()
{
_boxAnimator.SetTrigger("Open");
}

Resetting the game involves clearing out the gem order and setting the current gem number back to 0. Later on, the gem emission will also need to be reset, but that’s for another time.

    void ResetGame()
{
_currentGem = 0;
_enteredGemOrder = "";

//Reset the gem emission
}

Back in the Unity Editor, the actual gems can be hooked up with some components. Add the AR Selection Interactable and the Gem script to the gems. Each gem needs to have the color typed in the field for the Gem script component, which will act as a string to determine if the order is correct when selecting the gems. Each gem also needs a Select Entered Interactable Event for the actual box GameObject. The GemSelect method will be called here, with the color of the gem dragged into the respective fields.

With this, we have a working magic box set!

--

--