Combination Code

So i’ve been stuck for several hours trying to figure out how I can make the combination code to be corrected or incorrect. For some reason I only see “Wrong combination Code” and also my gameObject currentCombination int, change from its original number to 0. Any Ideas how might I fix this to correctly work? There is a lot of commenting out in here, just been testing out the possibilities:

So this is where the combination will be declared on the keypad
// This is for the KeyPad Buttons Area

			foreach(Keybutton button in buttons)
			{
				
				if(Input.GetMouseButtonDown(0)) {
					if (button.gameObject.Equals(hit.collider.gameObject))
					{
						hit.transform.renderer.material.color = Color.red;
						button.ButtonWasClicked();

		break;
					}
				}
			}

		}
	}

This is where the functions of the combination work and process

public class Keybutton
{
	public GameObject gameObject;
	//public int Numberbutton;
	private int Combination = 1234;
	public int currentCombination;
	private int buttonClickProgress;

	public void  ButtonWasClicked (){
		
		currentCombination = 0;
		//buttonClickProgress;
		
		if(buttonClickProgress < 2){
			currentCombination *= 1;
		}
		else{
			if(currentCombination == Combination){
				Debug.Log("You Opened the Combination lock!");
			}
			else{
				Debug.Log("Wrong Combination Code, reseting...");
			
				gameObject.SetActive(false);
			}
	}
}	
}

2 Answers

2

Firstly, you’re not adding to the current combination. Just look at line 15.

Secondly, you’re checking if you clicked one button 1234 times. Not if one “button” is 1, the other is 2, the third is 3 and the fourth is 4. Maybe you should rethink your logic on this?

So should I just make each gameObject button equal a number??

You didn't tell your number buttons are GameObjects. You should definately give more detail about what the situation here is and what you want to accomplish.

Sorry I didnt cover that, but I was just about to comment on your answer about, these buttons are gameObjects you click on. Each one is assigned a number 0-9. Do you know how else this can be done?

Should I change int Combination and int currentCombination to strings?

As for checking a passcode typed in on the keyboard:

using UnityEngine;
using System.Collections;

public class Code : MonoBehaviour 
{
	const string PASS_CODE = "1234";

	string _TypedCode = "";

	void Update() 
	{
		if (Input.inputString.Length > 0)
		{
			foreach (char c in Input.inputString)
			{
				_TypedCode += c;
			}
			
			if (_TypedCode.Length > PASS_CODE.Length)
			{
				_TypedCode = _TypedCode.Substring(_TypedCode.Length - PASS_CODE.Length);
			}
			
			if (_TypedCode == PASS_CODE)
			{
				Debug.Log("PASS CODE CORRECT");
			}
			else 
			{
				Debug.Log(_TypedCode + " is not the passcode");
			}
		}
	}
}