2D Puzzle Sequence Help

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!");
            }

        }
    }
}

Honestly, I would create an array and put all your gameobjects in that array in the order you want to trigger them. Then keep your int to keep track of what part of the sequence you’re on. If the int is 0 you’re on the first gameobject. If it’s 2, you’re on the third gameobject. Then just check if the gameobject matches, if not, loop through to reset the animations from where you’re at back through the array and then reset your int to 0 (assuming the wrong choice resets things).

Do you have the collider part working, too? Or need help with that?

Sorry for the super late response! I managed to figure out the issue after 4 more hours… haha. Everything is working as intended now including colliders!. I appreciate the feedback!

Cool, glad you got it working.