[SOLVED] Can't find errors CS1547, CS1525

Hello all, total beginner here with C# although it reminds me of programming in Basic on the TRS-80 back in the day. OK on to the problem. I have a problem with one of my states and I can’t find it. The compiler finds a problem with the “open” state in line 60 but I cannot figure it out. Is there some syntax thing that I am missing?

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

public class TextController : MonoBehaviour {

public Text text;
private enum States {room1, writing_desk, door_1, silver_key, note, open, escape}
private States myState;

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

// Update is called once per frame
void Update () {
	print (myState);
	if (myState == States.room1) {
		state_room1 ();
	} else if (myState == States.writing_desk) {
		state_writing_desk ();
	} else if (myState == States.door_1) {
		state_door_1 ();
	} else if (myState == States.silver_key) {
		state_silver_key ();
	} else if (myState == States.note) {
		state_note ();
	}else if (myState == States.open) {
		state_open ();
	}else if (myState == States.escape) {
		state_escape ();
	}
}

void state_room1 () {
	text.text = "You awaken to find yourself on the concrete floor of a room, it is dimmly lit and" +
				" cannot be any larger than a 20 foot box. In the center of the room to your right" +
				" is a small writing desk. \n\n" +
				"Press W to view the WRITING DESK, the number 1 to view the DOOR";
	if (Input.GetKeyDown (KeyCode.W)) {
		myState = States.writing_desk;
	} else if (Input.GetKeyDown (KeyCode.Alpha1)) {
		myState = States.door_1;
	}
}

void state_door_1 () {
	text.text = "A sturdy wooden door. There is nothing special about the door, other than what" +
		" looks like a small silver key hole. \n\n" +
		"Press W to view the WRITING DESK, R to RETURN, O to open the DOOR with the key.";
	if (Input.GetKeyDown (KeyCode.W)) {
		myState = States.writing_desk;
	} else if (Input.GetKeyDown (KeyCode.R)) {
		myState = States.room1;
	}else if (Input.GetKeyDown (KeyCode.O)) {
		myState = States.open;
}

void state_open () {
	text.text = "The rusty lock turns open with a crack and the door begins to swing into the" +
		" room with a disconcerting creek. \n\n" +
		"Press E to ESCAPE through the DOOR, R to RETURN,";
	if (Input.GetKeyDown (KeyCode.W)) {
		myState = States.writing_desk;
	} else if (Input.GetKeyDown (KeyCode.R)) {
		myState = States.room1;
	}
}

void state_writing_desk () {
	text.text = "One the top of the WRITING DESK you see a small siver key and a note. \n\n" +
	"Press K to take the SILVER Key, N to read the NOTE, R to RETURN";
	if (Input.GetKeyDown (KeyCode.K)) {
		myState = States.silver_key;
	} else if (Input.GetKeyDown (KeyCode.R)) {
		myState = States.room1;
	}else if (Input.GetKeyDown (KeyCode.N)) {
		myState = States.note;
	}
}

void state_silver_key () {
	text.text = "A small silver key with worn teeth. You place it in your INVENTORY \n\n" +
				"R to RETURN";
	if (Input.GetKeyDown (KeyCode.R)) {
		myState = States.room1;
	}
}

void state_note () {
	text.text = "You unfold a crumpled piece of paper. Written in red on the inside are" +
				" the following words... Welcome Mr. Smith I hope you enjoy my game. Solve the" + 
				" riddles that await you in each space and you will be rewarded. Fail and you" + 
				" shall perish \n\n" +
				"R to RETURN";
	if (Input.GetKeyDown (KeyCode.R)) {
		myState = States.room1;
	}
}

void state_escape () {
	text.text = "You walk slowly through the DOOR into the light of day, thankful that" +
		" you have survived the ordeal.\n\n" +
		"R to RETURN";
	if (Input.GetKeyDown (KeyCode.R)) {
		myState = States.room1;
	}
}

}

Hello @Christopher_Rogers, how are you?

The problem is that you haven’t closed the curly brace of the code within “state_door_1”, just add one more curly brace after the last if/else statement and it should run as expected

@Joao_Dalvi thank you for the help. I added the bracket as you suggested and now the program will run, which it did not before. However, it does not behave properly. It does not enter the starting States.room1 as it should, rather it returns the following error in the console infinitely:

NullReferenceException: Object reference not set to an instance of an object
TextController.state_room1 () (at Assets/TextController.cs:36)
TextController.Update () (at Assets/TextController.cs:19)

Here is the bit of code I changed from your suggestion:

void state_door_1 () {
text.text = “A sturdy wooden door. There is nothing special about the door, other than what” +
" looks like a small silver key hole. \n\n" +
“Press W to view the WRITING DESK, R to RETURN, O to open the DOOR with the key.”;
if (Input.GetKeyDown (KeyCode.W)) {
myState = States.writing_desk;
} else if (Input.GetKeyDown (KeyCode.R)) {
myState = States.room1;
} else if (Input.GetKeyDown (KeyCode.O)) {
myState = States.open;
}
}

Seems that the problem is with the text.text, have you dragged the actual text from the scene to this object inspector?

I believe so. I have attached a screen shot of the relevant part of the inspector. It was working before I started adding all of those additional States.

Under the Text Controller Script in the inspector there is a slot for a Text, just drag the Text component (just drag the name) from the inspector to this slot.

The problem is indeed this as I can see in your screenshot. You may take a look again at the last few lessons looking if you find the part that Ben drag the text

Thanks! I fixed it. I have no idea how the script got disconnected from the text element, but now I know to look there in the future. Hopefully these errors will get easier to spot. Thanks again!

1 Like

Public variables addressed in the inspector are very easy to break, it is possible to lose the value even if you only take out the word public, run the code and then make it public again.

1 Like