How to add a lock/key mechanic to Prison game?

Trying to set up a basic lock & key mechanic for the Text101 game where you can pick up an item in one state and use it to access an otherwise-inaccessible state.

Tried doing this by declaring a bool at start of TextController script, assigning false at start of play, setting it to true when you pick up the key, and then looking for both the KeyDown and bool == true to access the desired state, but it’s not working… syntax error or something more fundamental?

Here’s the code (w/ a simplified version of the Prison story). Can someone tell me where I went wrong?

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class TextController : MonoBehaviour {

public Text text;

bool key;

private enum States {
	room, door, door1, chest, hall, freedom}; 
private States myState;

// Use this for initialization
void Start () {
	key = false;
	myState = States.room;
}

// Update is called once per frame
void Update () {
	print (myState);
	if (myState == States.room) 			{state_room();}
	else if (myState == States.door) 		{state_door();}
	else if (myState == States.door1) 		{state_door1();} 
	else if (myState == States.chest) 		{state_chest();}
	else if (myState == States.freedom) 	{state_freedom();}
}

void state_room () {
	text.text = "You're in a cell with a locked door and a small chest against the wall. \n\n" +
				"Press D to try the door, or press C to look in the chest.";
	if (Input.GetKeyDown(KeyCode.D)) && key == true			{myState = States.door1;}
	else if (Input.GetKeyDown(KeyCode.D)) && key == false	        {myState = States.door;}
	else if (Input.GetKeyDown(KeyCode.C)) 					{myState = States.chest;} 	
}

void state_door () {
	text.text = "The door is locked. \n\n" +
			 "Press R to look around the room again.";
	if (Input.GetKeyDown(KeyCode.R)) 		{myState = States.room;}
}

void state_chest() {
	text.text = "The chest opens with a creak. There's a key inside! \n\n" +
			 "Press K to take the key and close the chest.";
	key = true;
	if (Input.GetKeyDown(KeyCode.K)) 		{myState = States.room;}
}

void state_door1 () {
	text.text = "You unlock the door with the key! \n\n" +
			 "Press X to open the door and exit the cell.";
	if (Input.GetKeyDown(KeyCode.X)) 		{myState = States.hall;}
}

void state_freedom () {
	text.text = "Freedom! \n\n" +
			 "Press R to restart.";
	if (Input.GetKeyDown(KeyCode.R)) 		{myState = States.room;}
}

}

The key, so to speak, is in your parenthasis.
Specifically this line.
if (Input.GetKeyDown(KeyCode.D)) && key == true

the if statement is not checking the status of key because it is outside the range of the closing parenthasis.
The fixed code would look like this.
if (Input.GetKeyDown(KeyCode.D) && key == true )

you could even shorten it to this.
if (Input.GetKeyDown(KeyCode.D) && key)
because if you simply state the name of a boolean in an if statement it automatically checks for truth.

Worked! That was easy! I had tried something like that earlier but added another set of ( ) - didn’t try using what was already there. Thanks for the help!

Privacy & Terms