Text101 script error

Help, after writing down everything according to lecture 35 section 3, some codes are in red color even after completing whole method.

The method names in red dont exist.

As an example you call state_in_closet() but yet you method (just on the bottom of the second screenshot) is called in_closet()

Line 39 and 44 respectively.

Hope this helps.

:disappointed: i don’t know how to fix it.

You either dont have the methods you beed, or they are incorrectly named.

The if statement which is determining which method to call based on the current state is trying ti call a method that doesnt exist.

So, for example, rename void in_closet() to void state_in_closet() which is the name of the method you are currently trying to call.

If you have named others differently from the names you are calling in your if statement then rename those also. If you haven’t created them ywt, create them with the same names as you are calling.

Make sense?

1 Like

I’ll try and see if it will fix this error. :neutral_face:

If you still have problems, post your complete code up, use the code formatting tool in the composer so its easier to read and I will let you know what else I can see.

1 Like

public class TextController : MonoBehaviour { public Text text;

private enum States {cell, sheets_0, sheets_1, lock_0, lock_1, mirror, cell_mirror, corridor_0, stair_0, stair_1, 
	stair_2, courtyard, floor, corridor_1, corridor_2, corridor_3, closet_door, in_closet
    };
private States myState;



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

}
// Update is called once per frame
void Update () {
	print (myState);
	if (myState == States.cell)                         {state_cell();}
	else if (myState == States.sheets_0)                {state_sheets_0();}
	else if (myState == States.sheets_1)                {state_sheets_1();}
	else if (myState == States.lock_0)                  {state_lock_0();}
	else if (myState == States.lock_1)                  {state_lock_1();}
	else if (myState == States.mirror)                  {state_mirror();} 
	else if (myState == States.cell_mirror)             {state_cell_mirror();}
	else if (myState == States.corridor_0)              {state_corridor_0();}
	else if (myState == States.stair_0)                 {state_stair_0();} 
	else if (myState == States.stair_1)                 {state_stair_1();} 
	else if (myState == States.stair_2)                 {state_stair_2();}
	else if (myState == States.courtyard)               {state_courtyard();}
	else if (myState == States.floor)                   {state_floor();}
	else if (myState == States.corridor_1)              {state_corridor_1();}
	else if (myState == States.corridor_2)              {state_corridor_2();} 
	else if (myState == States.corridor_3)              {state_corridor_3();}
	else if (myState == States.closet_door)             {state_closet_door();}
	else if (myState == States.in_closet)               {state_in_closet();} 


}
  
void in_closet() {
	text.text = "Inside the closet you see a cleaner's uniform that looks about your size! " +
		        "Seems like your day is looking-up.\n\n" +
			    "Press D to Dress up, or R to Return to the corridor";
	if (Input.GetKeyDown(KeyCode.R)) {myState = States.corridor_2;} 
	else if (Input.GetKeyDown(KeyCode.D)) {myState = States.corridor_3;}
}

void closet_door() {
	text.text = "You are looking at a closet door, unfortunately it's locked. " +
		        "Maybe you could find something around to help enourage it open?\n\n" +
			    "Press R to Return to the corridor";
	if (Input.GetKeyDown(KeyCode.R)) {myState = States.corridor_0;}
}

void corridor_3() {
	text.text = "You're standing back in the corridor, now convincingly dressed as a cleaner. " +
		        "You strongly consider the run for freedom.\n\n" +
			    "Press S to take the Stairs, or U to Undress";
	if (Input.GetKeyDown(KeyCode.S)) {myState = States.courtyard;} 
	else if (Input.GetKeyDown(KeyCode.U)) {myState = States.in_closet;}
}

void corridor_2() {
	  text.text = "Back in the corridor, having declined to dress-up as a cleaner.\n\n" +
		          "Press C to revisit the Closet, and S to climb the stairs"; 
	              if (Input.GetKeyDown(KeyCode.C)) {myState = States.in_closet;}
	              else if (Input.GetKeyDown(KeyCode.S)) {myState = States.stair_2;}
}

void state_cell() {
	text.text = "You are in a prison cell, and you want to escape. There are " +
		        "some dirty sheets on the bed, a mirror on the wall, and the door " + 
			    "is locked from the outside.\n\n" +
			    "Press S to view Sheets, M to view Mirror and L to view Lock" ;
	if (Input.GetKeyDown(KeyCode.S))             {myState = States.sheets_0;}
	else if (Input.GetKeyDown(KeyCode.M))        {myState = States.mirror;}
	else if (Input.GetKeyDown(KeyCode.L))        {myState = States.lock_0;}

}

void corridor_1() {
	text.text = "Still in the corridor. Floor still dirty. Hairclip in hand. " +
		        "Now what? You wonder if that lock on the closet would succumb to " + 
			    "to some lock-picking?\n\n" +
			    "P to Pick the lock, and S to climb the stairs";
	if (Input.GetKeyDown(KeyCode.P))            {myState = States.in_closet;}
	else if (Input.GetKeyDown(KeyCode.S))       {myState = States.stairs_1;}
}
		
void floor () {
	text.text = "Rummagaing around on the dirty floor" + 
		        "you find a hairclip.\n\n" + 
			    "standing, or H to take the Hairclip." 
	            "Press R to Return to the";
	if (Input.GetKeyDown(KeyCode.R))            {myState = States.corridor_0;}
	else if (Input.GetKeyDown(KeyCode.H))       {myState = States.corridor_1;}

}

void courtyard () {
	text.text = "You walk through the courtyard dressed as a cleaner. " +
		        "The guard tips his hat at you as you waltz past, claiming " + 
			    "your freedom. You heart races as you walk into the sunset.\n\n" +
			    "Press P to Play again." ;
	if (Input.GetKeyDown(KeyCode.P)) {myState = States.cell;} 
}

void stairs_0 () {
	text.text = "You start walking up the stairs towards the outside light. " +
		        "You realise it's not break time, and you'll be caught immediately. " +
			    "You slither back down the stairs and reconsider.\n\n" +
			    "Press R to Return to the corridor." ;
	if (Input.GetKeyDown(KeyCode.R)) {myState = States.corridor_0;} 
}

void stairs_1 () {
	text.text = "Unfortunately weilding a puny hairclip hasn't given you the " +
		        "confidence to walk out into a courtyard surrounded by armed guards!\n\n" +
			    "Press R to Retreat down the stairs" ;
	if (Input.GetKeyDown(KeyCode.R)) {myState = States.corridor_1;}
}

void stairs_2() {
	text.text = "You feel smug for picking the closet door open, and are still armed with " +
		        "a hairclip (now badly bent). Even these achievements together don't give " +
			    "you the courage to climb up the staris to your death!\n\n" +
			    "Press R to Return to the corridor";
	if (Input.GetKeyDown(KeyCode.R)) {myState = States.corridor_2;}
}

void state_mirror() {
	text.text = "The dirty old mirror on the wall seems loose.\n\n" +
		        "Press T to Take the mirror, or R to Return to cell" ; 
	if (Input.GetKeyDown(KeyCode.T)) {myState = States.cell_mirror;} 
	else if (Input.GetKeyDown(KeyCode.R)) {myState = States.cell;}
}
void state_sheets_0() {
	text.text = "You can't believe you sleep in these things. Surely it's " +
		        "time somebody changed them. The pleasures of prison life " + 
			    "I guess!\n\n" +
			    "Press R to Return to roaming your cell" ;
	if (Input.GetKeyDown(KeyCode.R)) {myState = States.cell;} 
}

void state_sheets_1() {
	text.text = "Holding a mirror in your hand doesn't make the sheets look " +
		        "any better.\n\n" +
			    "Press R to Return to roaming your cell" ;
	if (Input.GetKeyDown(KeyCode.R)) {myState = States.cell;} 
}

void state_lock_0() {
	text.text = "This is one of those button locks. You have no idea what the " +
		        "combination is. You wish you could somehow see where the dirty " +
			    "fingerprints were, maybe that would help.\n\n" +
			    "Press R to Return to roaming your cell" ;
	if (Input.GetKeyDown(KeyCode.R)) {myState = States.cell;} 
}

void state_lock_1() {
	text.text = "You carefully put the mirror through the bars, and turn it round " +
		        "so you can see the lock. You can just make out fingerprints around " + 
			    "the buttons. You press the dirty buttons, and hear a click.\n\n" +
			    "Press O to Open, or R to Return to your cell" ;
	if (Input.GetKeyDown(KeyCode.O))      {myState = States.corridor_0;}
	else if (Input.GetKeyDown(KeyCode.R)) {myState = States.cell_mirror;}
}

void state_cell_mirror() {
	text.text = "You are still in your cell, and you STILL want to escape! There are " +
		        "some dirty sheets on the bed, a mark where the mirror was, " + 
			    "and that pesky door is still there, and firmly locked!\n\n" + 
			    "Press S to view Sheets, or L to view Lock" ;
	if (Input.GetKeyDown(KeyCode.S))          {myState = States.sheets_1;}
	else if (Input.GetKeyDown(KeyCode.L))     {myState = States.lock_1;}
}

void state_corridor_0() {
	text.text = "You are FREE!\n\n" +
				"Press P to Play again"; 
	if (Input.GetKeyDown(KeyCode.P))          {myState = States.cell;}
}

}

Yes and here is the screen shot too.

Hi,

So, the problem remains the same… I will explain;

Here’s a snippet from your logic in the Update() method;

     if (myState == States.cell)           {state_cell();}
else if (myState == States.sheets_0)       {state_sheets_0();}

In the brackets for the if statement, you are checking to see if your variable myState is equal to one of your specific values from your States enum. If it is, you are then calling the method in the curly braces { }

So, taking the first line as an example, if myState is equal to States.cell then, call the method named state_cell()

Looking at your code, all of your method calls in the if statement logic within the Update() method are still preceded with the state_ text, yet if you look at the names of the actual methods below, they are not.

So, first step, remove state_ from the method calls in the curly braces { } within the Update() method.

Next, look at the names of your methods and check that they are spelt the same, for example, you call stair_0() however you method is currently called stairs_0(). This has most likely happened if you typed the first part in perhaps yourself but then copy and pasted the rest of the code in from the lecture resources.

Once you have these names correctly named and being called this should be ok.

1 Like

I fixed it, the mistake was from 29-39 i forgot to add “state”. But in the game scene it is showing (error complier).

Are you saying after renaming the methods so that they align with the method calls (in the Update() method) you are still receiving an error message? If so, can you provide details of the error message, ideally with a screenshot.

Yes, there is no error in the script, i checked all. Even on the game scene it is not showing where exactly the error is… …

Can you paste your full code back up please.

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

public class TextController : MonoBehaviour { public Text text;
	
	private enum States {cell, sheets_0, sheets_1, lock_0, lock_1, mirror, cell_mirror, corridor_0, stair_0, stair_1, 
		stair_2, courtyard, floor, corridor_1, corridor_2, corridor_3, closet_door, in_closet
	};
	private States myState;
	
	
	
	// Use this for initialization
	void Start () {
		myState = States.cell; 
		
	}
	// Update is called once per frame
	void Update () {
		print (myState);
		if (myState == States.cell)                         {state_cell();}
		else if (myState == States.sheets_0)                {state_sheets_0();}
		else if (myState == States.sheets_1)                {state_sheets_1();}
		else if (myState == States.lock_0)                  {state_lock_0();}
		else if (myState == States.lock_1)                  {state_lock_1();}
		else if (myState == States.mirror)                  {state_mirror();} 
		else if (myState == States.cell_mirror)             {state_cell_mirror();}
		else if (myState == States.corridor_0)              {state_corridor_0();}
		else if (myState == States.stair_0)                 {state_stair_0();} 
		else if (myState == States.stair_1)                 {state_stair_1();} 
		else if (myState == States.stair_2)                 {state_stair_2();}
		else if (myState == States.courtyard)               {state_courtyard();}
		else if (myState == States.floor)                   {state_floor();}
		else if (myState == States.corridor_1)              {state_corridor_1();}
		else if (myState == States.corridor_2)              {state_corridor_2();} 
		else if (myState == States.corridor_3)              {state_corridor_3();}
		else if (myState == States.closet_door)             {state_closet_door();}
		else if (myState == States.in_closet)               {state_in_closet();} 
		
		
	}
	
	void state_in_closet() {
		text.text = "Inside the closet you see a cleaner's uniform that looks about your size! " +
			"Seems like your day is looking-up.\n\n" +
				"Press D to Dress up, or R to Return to the corridor";
		if (Input.GetKeyDown(KeyCode.R)) {myState = States.corridor_2;} 
		else if (Input.GetKeyDown(KeyCode.D)) {myState = States.corridor_3;}
	}
	
	void state_closet_door() {
		text.text = "You are looking at a closet door, unfortunately it's locked. " +
			"Maybe you could find something around to help enourage it open?\n\n" +
				"Press R to Return to the corridor";
		if (Input.GetKeyDown(KeyCode.R)) {myState = States.corridor_0;}
	}
	
	void state_corridor_3() {
		text.text = "You're standing back in the corridor, now convincingly dressed as a cleaner. " +
			"You strongly consider the run for freedom.\n\n" +
				"Press S to take the Stairs, or U to Undress";
		if (Input.GetKeyDown(KeyCode.S)) {myState = States.courtyard;} 
		else if (Input.GetKeyDown(KeyCode.U)) {myState = States.in_closet;}
	}
	
	void state_corridor_2() {
		text.text = "Back in the corridor, having declined to dress-up as a cleaner.\n\n" +
			"Press C to revisit the Closet, and S to climb the stairs"; 
		if (Input.GetKeyDown(KeyCode.C)) {myState = States.in_closet;}
		else if (Input.GetKeyDown(KeyCode.S)) {myState = States.stair_2;}
	}
	
	void state_cell() {
		text.text = "You are in a prison cell, and you want to escape. There are " +
			"some dirty sheets on the bed, a mirror on the wall, and the door " + 
				"is locked from the outside.\n\n" +
				"Press S to view Sheets, M to view Mirror and L to view Lock" ;
		if (Input.GetKeyDown(KeyCode.S))             {myState = States.sheets_0;}
		else if (Input.GetKeyDown(KeyCode.M))        {myState = States.mirror;}
		else if (Input.GetKeyDown(KeyCode.L))        {myState = States.lock_0;}
		
	}
	
	void state_corridor_1() {
		text.text = "Still in the corridor. Floor still dirty. Hairclip in hand. " +
			"Now what? You wonder if that lock on the closet would succumb to " + 
				"to some lock-picking?\n\n" +
				"P to Pick the lock, and S to climb the stairs";
		if (Input.GetKeyDown(KeyCode.P))            {myState = States.in_closet;}
		else if (Input.GetKeyDown(KeyCode.S))       {myState = States.stairs_1;}
	}
	
	void state_floor () {
		text.text = "Rummagaing around on the dirty floor" + 
			"you find a hairclip.\n\n" + 
				"standing, or H to take the Hairclip." 
				"Press R to Return to the";
		if (Input.GetKeyDown(KeyCode.R))            {myState = States.corridor_0;}
		else if (Input.GetKeyDown(KeyCode.H))       {myState = States.corridor_1;}
		
	}
	
	void state_courtyard () {
		text.text = "You walk through the courtyard dressed as a cleaner. " +
			"The guard tips his hat at you as you waltz past, claiming " + 
				"your freedom. You heart races as you walk into the sunset.\n\n" +
				"Press P to Play again." ;
		if (Input.GetKeyDown(KeyCode.P)) {myState = States.cell;} 
	}
	
	void state_stair_0 () {
		text.text = "You start walking up the stairs towards the outside light. " +
			"You realise it's not break time, and you'll be caught immediately. " +
				"You slither back down the stairs and reconsider.\n\n" +
				"Press R to Return to the corridor." ;
		if (Input.GetKeyDown(KeyCode.R)) {myState = States.corridor_0;} 
	}
	
	void state_stair_1 () {
		text.text = "Unfortunately weilding a puny hairclip hasn't given you the " +
			"confidence to walk out into a courtyard surrounded by armed guards!\n\n" +
				"Press R to Retreat down the stairs" ;
		if (Input.GetKeyDown(KeyCode.R)) {myState = States.corridor_1;}
	}
	
	void state_stair_2() {
		text.text = "You feel smug for picking the closet door open, and are still armed with " +
			"a hairclip (now badly bent). Even these achievements together don't give " +
				"you the courage to climb up the staris to your death!\n\n" +
				"Press R to Return to the corridor";
		if (Input.GetKeyDown(KeyCode.R)) {myState = States.corridor_2;}
	}
	
	void state_mirror() {
		text.text = "The dirty old mirror on the wall seems loose.\n\n" +
			"Press T to Take the mirror, or R to Return to cell" ; 
		if (Input.GetKeyDown(KeyCode.T)) {myState = States.cell_mirror;} 
		else if (Input.GetKeyDown(KeyCode.R)) {myState = States.cell;}
	}
	void state_sheets_0() {
		text.text = "You can't believe you sleep in these things. Surely it's " +
			"time somebody changed them. The pleasures of prison life " + 
				"I guess!\n\n" +
				"Press R to Return to roaming your cell" ;
		if (Input.GetKeyDown(KeyCode.R)) {myState = States.cell;} 
	}
	
	void state_sheets_1() {
		text.text = "Holding a mirror in your hand doesn't make the sheets look " +
			"any better.\n\n" +
				"Press R to Return to roaming your cell" ;
		if (Input.GetKeyDown(KeyCode.R)) {myState = States.cell;} 
	}
	
	void state_lock_0() {
		text.text = "This is one of those button locks. You have no idea what the " +
			"combination is. You wish you could somehow see where the dirty " +
				"fingerprints were, maybe that would help.\n\n" +
				"Press R to Return to roaming your cell" ;
		if (Input.GetKeyDown(KeyCode.R)) {myState = States.cell;} 
	}
	
	void state_lock_1() {
		text.text = "You carefully put the mirror through the bars, and turn it round " +
			"so you can see the lock. You can just make out fingerprints around " + 
				"the buttons. You press the dirty buttons, and hear a click.\n\n" +
				"Press O to Open, or R to Return to your cell" ;
		if (Input.GetKeyDown(KeyCode.O))      {myState = States.corridor_0;}
		else if (Input.GetKeyDown(KeyCode.R)) {myState = States.cell_mirror;}
	}
	
	void state_cell_mirror() {
		text.text = "You are still in your cell, and you STILL want to escape! There are " +
			"some dirty sheets on the bed, a mark where the mirror was, " + 
				"and that pesky door is still there, and firmly locked!\n\n" + 
				"Press S to view Sheets, or L to view Lock" ;
		if (Input.GetKeyDown(KeyCode.S))          {myState = States.sheets_1;}
		else if (Input.GetKeyDown(KeyCode.L))     {myState = States.lock_1;}
	}
	
	void state_corridor_0() {
		text.text = "You are FREE!\n\n" +
			"Press P to Play again"; 
		if (Input.GetKeyDown(KeyCode.P))          {myState = States.cell;}
	}
}

Hi @Sabeen_Moin,

See the comment in the code below;

void state_floor()
{
    text.text = "Rummagaing around on the dirty floor" +
        "you find a hairclip.\n\n" +
            "standing, or H to take the Hairclip."  // missing + sign on the end here

            "Press R to Return to the";
    if (Input.GetKeyDown(KeyCode.R)) { myState = States.corridor_0; }
    else if (Input.GetKeyDown(KeyCode.H)) { myState = States.corridor_1; }

}

This explains why the code wont build correctly, not sure the text there reads quite right for the story though.

1 Like

The problem is solved now :slight_smile: Thank you!

1 Like

Great! :slight_smile: Glad you have got it working now.

If you have any other issues let us know and we will see if we can work through them. Hopefully the experience of going through the above will be of use as it will give you a few things to check for going forwards :slight_smile:

1 Like

Privacy & Terms