[SOLVED] How can I ask user for a number?

I wanted to alternate the Prison game, so that player needs to know the code (a number, that he gets to know after looking in the mirror) to open the door.

So something, like ‘cin>>variable’ in C++ ,and then an If statement to check if it is a correct or wrong number.

Is that possible in this type of game in C# / Unity? Thanks in advance :slight_smile:

void state_door()
{
text.text = “You notice, that the door can be opened with a code.\n\n”;

if (know==false){	//know is a bool variable, that changes after you've seen the code
	text.text = text.text + "As for now, you rather not try every possible combination and decide to go back";
	
	if(Input.GetKeyDown(KeyCode.W)){ //after clicking W, user goes to the previous state
		myState=States.cell;}
}
else{
	text.text = text.text + "You probably know the code...\n\n"+
				"CODE: "; //here I'd like to ask for code and include an If statement and loop
}
1 Like

There is an easy way where you can use things that you already know… well it’s kind complicated but I don’t want to be a spoiler for later lessons.

You can use for example 4 booleans for 4-digit code.
Turn true every boolean for every true digit the user press and turn false all booleans in case a false enter.

code like:

bool digit1, digit2, digit3, digit4;

void Start(){
           digit1 = false;
           digit2 = false;
           digit3 = false;
           digit4 = false;
}

void Update(){
      if(Input.anyKey){
           if(Input.GetKeyDown (KeyCode.1 && digit1 = false)){
                digit1 = true;
           }else if(Input.GetKeyDown (KeyCode.2) && digit2 = false && digit1 = true){
                digit2 = true;
           }else if(Input.GetKeyDown (KeyCode.3) && digit2 = true && digit3 = false){
                digit3 = true;
           }else if(Input.GetKeyDown (KeyCode.4) && digit 3= true && digit4 = false){
                // Oped that door or whatever you want
           }else{                             //if something else pressed then turn all false and start over
                digit1 = false;
                digit2 = false;
                digit3 = false;
          }
     }
}

Well… I didn’t try it to be sure if it works, but I think you understand me what I’m saying here. It will be more realistic… like a numepad without a screen like most alarm/lock-pads.

You can have a text to print false entry… but I think I gave you some food for thought! Be creative like Ben says all the time! :smile:

2 Likes

As an alternative;

1 Like

Thank you, Im going to try both options :smile:

1 Like

Great.

Post up if you have any difficulties and I’m sure there will be some help available :slight_smile:

The link to the github repository contains a fairly simple but working example of data entry / response.

Privacy & Terms