Home > front end >  Combination lock Code not working; Final Combination giving error upon run in Unity
Combination lock Code not working; Final Combination giving error upon run in Unity

Time:09-17

I'm currently working on an assignment where i need to make a working lock, and i'm doing in unity (i'm relatively new, not having much experience in unity). I've been following a tutorial but i think i went wrong somewhere. I'm not sure what it is but i get the error on two instances where rotate is mentioned. the error is "The name 'Rotate' does not exist in the current context" but as far as i can tell, i've followed the tutorial to the bone. - The code for the combo is this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LockControl : MonoBehaviour
{

private int[] result, correctCombination;
private void Start() {
    result = new int[]{1,1,1};
    correctCombination = new int[] { 3, 7, 9 };
    Rotate.Rotated  = CheckResults;
}


private void CheckResults(string wheelName, int number) {
    switch (wheelName)
    {
        case "wheel1":
            result[0] = number;
            break;

        case "wheel2":
            result[1] = number;
            break;
        
        case "wheel3":
            result[2] = number;
            break;
    }
    if (result[0] == correctCombination[0] && result[1] == correctCombination[1] && result[2] == correctCombination[2])
    {
        Debug.Log("Opened!");
    }
}

private void OnDestroy() {
    Rotate.Rotated -= CheckResults;
    }
}

And the Lock rotation code is this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class RotateScript : MonoBehaviour
{


public static event Action<string, int> Rotated = delegate { };

private bool coroutineAllowed;

private int numberShown;

// Start is called before the first frame update
private void Start()
{
    coroutineAllowed = true;
    numberShown = 1;
}

private void OnMouseDown() {
    if (coroutineAllowed)
        StartCoroutine("RotateWheel");
    
}

// Update is called once per frame
private IEnumerator RotateWheel()
{
    coroutineAllowed = false;

    for (int i = 0; i <= 11; i  ) {
        transform.Rotate(0f, 0f, 3f);   
        yield return new WaitForSeconds(0.01f);
    }
    coroutineAllowed = true;

    numberShown  = 1;
    
    
    if (numberShown > 9) {
        numberShown = 0;
    }
    Rotated(name, numberShown);
}
}

The error is within the combination code, and I can't get it to work. I've tried Searching but i can't find an answer and was hoping to get some help here, whether i need to declare something, fix a mistake or just restart from how incorrect it is.

Thanks!

CodePudding user response:

As I mentioned in my comment, I believe the issue is you have improperly named the script from the tutorial you are following. The error is quite straightforward in saying that there is nothing declared with the reference name Rotate.

To solve the issue, you will either need to rename both your class and .cs file name to Rotate, or an easier solution, change the lines:

Rotate.Rotated  = CheckResults;
Rotate.Rotated -= CheckResults;

to

RotateScript.Rotated  = CheckResults;
RotateScript.Rotated -= CheckResults;
  • Related