Hello,
I’m currently working on a small RPG prototype for a game idea, and I’m stuck on a specific problem. In this prototype I have a puzzle mechanic, the idea is that the player can move into a trigger collider and interact with one of seven pylons. These pylons have a deactivated and activated animations tied to them. When the player interacts with a pylon it switches from deactivated to activated. However, I am trying to create a script that has the player activate the pylons in the correct order in order to recieve a reward. I would like the pylons to activate and add to the sequence, but if the player gets the sequence wrong then the pylons reset. If the player gets the sequence correct it would unlock the reward.
Below is the script I have currently, but I am stuck. It technically works to some extent but I know there has to be a better more efficent way to write this. I am really new to C# so if anyone could help walk me through and understand what to do that would be great!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PuzzleSelection : MonoBehaviour {
public static string correctSequence = "7321654";
public static string playerSequence = "";
public static int totalDigits = 0;
public GameObject pylon01;
public GameObject pylon02;
public GameObject pylon03;
public GameObject pylon04;
public GameObject pylon05;
public GameObject pylon06;
public GameObject pylon07;
void Start () {
}
void Update () {
if (pylon01 && Input.GetKeyDown (KeyCode.E)) {
playerSequence += gameObject.name;
totalDigits += 1;
Debug.Log ("1");
}
if (pylon02 && Input.GetKeyDown (KeyCode.E)) {
playerSequence += gameObject.name;
totalDigits += 1;
Debug.Log ("2");
}
if (pylon03 && Input.GetKeyDown (KeyCode.E)) {
playerSequence += gameObject.name;
totalDigits += 1;
Debug.Log ("3");
}
if (pylon04 && Input.GetKeyDown (KeyCode.E)) {
playerSequence += gameObject.name;
totalDigits += 1;
Debug.Log ("4");
}
if (pylon05 && Input.GetKeyDown (KeyCode.E)) {
playerSequence += gameObject.name;
totalDigits += 1;
Debug.Log ("5");
}
if (pylon06 && Input.GetKeyDown (KeyCode.E)) {
playerSequence += gameObject.name;
totalDigits += 1;
Debug.Log ("6");
}
if (pylon07 && Input.GetKeyDown (KeyCode.E)) {
playerSequence += gameObject.name;
totalDigits += 1;
Debug.Log ("7");
}
}
void LateUpdate() {
if (totalDigits == 7)
{
if (playerSequence == correctSequence) {
Debug.Log ("Correct!");
}
else
{
playerSequence = "";
totalDigits = 0;
Debug.Log ("Try Again!");
}
}
}
}