[Solved] Five Different Compiler errors

Hey, sorry to inconvenience you but I seem to have hit a road block in the course for I cannot figure out how to solve these errors in my code.
Assets/TextController.cs(41,26): error CS1525: Unexpected symbol if' Assets/TextController.cs(26,33): error CS1525: Unexpected symbol(’, expecting )',,’, ;',[’, or =' Assets/TextController.cs(26,31): error CS1547: Keywordvoid’ cannot be used in this context
Assets/TextController.cs(18,21): error CS1525: Unexpected symbol (', expecting)’, ,',;’, [', or=‘
Assets/TextController.cs(18,19): error CS1547: Keyword `void’ cannot be used in this context

here you can find a link to my code:

Thank you so much!

edit: I am using unity 4.6.9

Sorry accidentally posted this early, I am working on it right now brb

Hi @LiamGl,

You Start() method is missing it’s closing } character.

As is your update() method.

Your update() method also needs to be renamed to Update() as C# is case-sensitive.

You also have a missing ; character at the end of the first statement in your method states_sheet_0().

You also have a typo indicated below when you call your state_sheets_0() method.

There is also a space character that you do not need between text. text as indicated below.

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

public class TextController : MonoBehaviour {

	public Text text;
	
	private enum States {cell, mirror, sheets_0, lock_0, cell_mirror, sheets_1, lock_1, freedom};
	private States myState;	
	
	// Use this for initialization
	void Start () 
	{
		myState = States.cell;
	// missing } here
	
	// Update is called once per frame
 	void update ()     // uppercase the "U"
	{
		print (myState);
		
		myState = States.cell;
		
		if (myState == States.cell) 
		{
			state_cell ();
		} 
		else if (myState == States.sheets_0) 
		{
			States.sheets_0 ();    // this line should read "state_sheets_0();" e.g. the name of the method you want to call
		}
	// missing } here
			
	void state_cell ()
	{
                // remove the space between the "text. text", so that it reads "text.text"
		text. text = "You are in a prison cell, it's been years " +
					 "of pain and boredom, i'ts been enough. There's " +
					 "some dirty sheets on your bed, a broken mirror " +
					 "on your wall, and a door infront of you. Locked. \n\n" +
					 "Press A to inspect sheets \n" +
					 "Press S to inspect broken mirror \n" +
					 "Press D to inspect locked door";
					 
		if (Input.GetKeyDown(KeyCode.A)) 
		{
			myState = States.sheets_0;
		}
	}
	
	void state_sheets_0 ()
	{ 
		Text.text = "Nothing here, you cannot believe you actually slept in those. \n" +
					"Press R to return to roaming your cell"	// missing ; at the end of this line
					
		if (Input.GetKeyDown(KeyCode.R)) 
		{
			myState = States.cell;
		}
	}
	
// missing } here

Incidentally, you can post our code directly into the forum here.


See also;

Hi Rob
Thank you so much for the help! sadly after doing fixing all of those problems I had one left, I managed to fix that myself but it still left me with two more errors.

Assets/TextController.cs(31,33): error CS0103: The name `State_sheets_0’ does not exist in the current context

Assets/TextController.cs(53,38): error CS0120: An object reference is required to access non-static member `UnityEngine.UI.Text.text’

here is an updated version of my code

‘’’

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

public class TextController : MonoBehaviour {

public Text text;

private enum States {cell, mirror, sheets_0, lock_0, cell_mirror, sheets_1, lock_1, freedom};
private States myState;	

// Use this for initialization
void Start () 
{
	myState = States.cell;
	}
	
	// Update is called once per frame
	void Update ()   
	{
		print (myState);
		
		myState = States.cell;
		
		if (myState == States.cell) 
		{
			state_cell ();
		} 
		else if (myState == States.sheets_0) 
		{
			State_sheets_0 (); 
		}
		}
		
		void state_cell ()
		{
			text.text = "You are in a prison cell, it's been years " +
				"of pain and boredom, i'ts been enough. There's " +
					"some dirty sheets on your bed, a broken mirror " +
					"on your wall, and a door infront of you. Locked. \n\n" +
					"Press A to inspect sheets \n" +
					"Press S to inspect broken mirror \n" +
					"Press D to inspect locked door";
			
			if (Input.GetKeyDown(KeyCode.A)) 
			{
				myState = States.sheets_0;
			}
		}
		
		void state_sheets_0 ()
		{ 
			Text.text = "Nothing here, you cannot believe you actually slept in those. \n" +
						"Press R to return to roaming your cell";	
	}	
}					

‘’'
Once again, thank you for the help you have already given me! :slight_smile:

Hi,

So, remember how I mentioned C# was case-sensitive in my previous post :slight_smile:

If you look at the first error the compiler is telling you that it can’t find a method called “States_sheets_0()”. If you look at your code you have a method called “states_sheets_0()”.

To emphasise the difference;

States_sheets_0()
states_sheets_0()

So, in your Update() method, simply lower-case the “S” in the methd call for States_sheets_0();

Regarding the second issue, an object reference required implies that for something it cannot refer the object. It refers to Text.text - this is the text property of the object of type Text.

If we look at your code to see where that is used we can see that you set the text that you want to appear on the screen in two places, void state_cell() and void states_sheets_0(), both of these methods are trying to access the text property of the a Type type object, which, in this case, just to blow your mind is actually named “text” also.

So, look to the top of your script where you declare this variable, it all looks good. You made it public, now why was that? This was so that it was accesible from outside of this class. What would want to access it? The Inspector within the Unity editor in this case.

If you selected your TextController game object in the Hierarchy and then click on the Inspector you will see that your publicly exposed variable called “text” is displayed as a field which I suspect is empty. What you need to do here is drag the UI Text game object which you added to display the text in the scene from the Hierarchy into this field.

This will create the reference which your error message is stating it is missing.

Also, if you haven’t already, please do read the link I gave you for formatting your code when you post in the forum, it makes it a lot easier to read for other people who may be trying to help you :slight_smile:

Hope this helps :slight_smile:

Thank you :slight_smile:

1 Like

You are very welcome Liam.

Privacy & Terms