its says void cant be used in that context on line 51⌠also the âstate_lock_0â is red and i dont understand why
It looks like you are missing some closing braces â}â
It appears you are missing two. On line 29 you also have an invalid function.
Try adding a curly closing brace on line 28 and line 51.
well that fixed it. im not surprised that i missed that tbhâŚ
i appreciate the help James!
No problem
These are common mistakes when starting out, heck I STILL do it sometimes. Itâs also part of the reason I donât like inline braces because itâs harder to line up the braces.
example:
You code leading up to line 52 is like this
void state_sheets_1() { text.text = "all your test is here, blah blah blabh"; if (Input.GetKeyDown(KeyCode.R)) {myState = States.cell;} //You are missing the closing brace for the above function here. void state_lock_0() { text.text = //rest of code blah blah
Doing in line code it can make it hard to read. Another way to write the code to be able to read the braces better would be like this.
> void state_sheets_1()
> {
> text.text = "all your test is here, blah blah blabh";
> if (Input.GetKeyDown(KeyCode.R))
> {
> myState = States.cell;
> }
> } // you can see that this bracket would be missing because it lines up with it's opening brace.
>
> void state_lock_0()
> {
> text.text = //rest of code blah blah
Doing like above allows all opening and closing brackets and braces to line up vertically. I donât use mono develop, but Visual Studio also explicitly tells you that itâs âexpecting a }â when one it missing.
Of course you will use more space if you have a separate line for each bracket or brace, but in a IDE like Visual Studio where you can collapse functions easily, the benefit of readability makes up for the extra vertical length.
the only reason i use mono is because he said there would be differences when i first started the course. i was using VS at first but i didnt want to make a mistake because of a difference and not know why there was a mistake.
I didnât see much of a difference when I did the course using VS. I was also using Unity 2017 however and there was some significant differences there of course, but Visual Studio (with the Unity package installed) was pretty good and pointing out when I was using obsolete code and suggested what to use instead.
when you say significant differences when using unity 2017, what do you mean exactly?
If you have a look at lecture 8 in the course you will see under Resources that there is a link to a Google Doc, this covers all of the changes in the code that will be required if you do not use the same version of Unity as the course does. Since version 4.6 there have been numerous API changes which mean if you are using a newer version of Unity, the code which you see in the course may not align exactly with what you need to use for a new Unity version.
Hope this helps
Side note - itâs typically always easier for everyone who helps you if you copy/paste your code into your posts rather than pop screenshots up. It allows them to copy/paste chunks of your code back into a reply, rather than having to type it all from the beginning. Screenshots are however really useful when it comes to providing some information from the Unity editor, such as values in the Inspector, Hierarchy or console.
See also;
- Forum User Guides : How to apply code formatting within your post
okay so i transered everything into unity 2017, as well as visual studio 17.
there are still some errors that i dont seem to understandâŚ
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);
if (myState == States.cell)
{
State_cell();
}
else if (myState == States.mirror)
{
State_mirror();
}
else if (myState == States.sheets_0)
{
State_sheets_0();
}
else if (myState == States.lock_0)
{
State_lock_0();
}
else if (myState == States.cell_mirror)
{
State_cell_mirror();
}
else if (myState == States.sheets_1)
{
State_sheets_1();
}
else if (myState == States.lock_1)
{
State_lock_1();
}
else if (myState == States.freedom)
{
State_freedom();
}
}
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;}
}
void State_sheets_0()
{
text.text = "You can't beleive you sleep in these things. Surely it's " +
"time somebody changed them. The pleasures of prison life " +
"is locked from the outside " +
"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. " +
"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 coluld 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 seet he 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.R)) {myState = States.freedom;}
else if (Input.GetKeyDown(KeyCode.R)) {myState = States.cell_mirror;}
}
void State_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_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";
}
void State_freedom()
{
text.text = "You are FREE!\n\n" +
"Press P to Play again";
if (Input.GetKeyDown(KeyCode.P))
{
myState.cell();
}
}
}
can you assist me please i dont seem to understand why i keep getting errors.
Could you let us know what the errors you are receiving are, please.
Incidentally, in your method State_lock_1
you are checking for the same KeyCode but to do different things.
Within your method State_freedom
you are using a bit of a mixture between the state enum
and a method call, I believe you actually want this;
myState = States.cell;
okay so i upgraded to visual studio and unity 2017 and completly restarted my game. i am getting errors saying âvoid can be used in this contextâ.
can you please explain to me why.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TextController : MonoBehaviour {
public Text text;
private enum States {cell, mirror, sheet_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);
if (myState == States.cell)
{
state_cell();
}
else if (myState == States.mirror)
{
state_mirror();
}
else if (myState == States.sheets_0)
{
state_sheets_0();
}
else if (myState == States.lock_0)
{
state_lock_0();
}
else if (myState == States.cell_mirror)
{
state_cell_mirror();
}
else if (myState == States.sheets_1)
{
state_sheets_1();
}
else if (myState == States.lock_1)
{
state_lock_1();
}
else if (myState == States.freedom)
{
state_freedom();
}
}
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.sheet_0;
}
else if (Input.GetKeyDown(KeyCode.M))
{
myState = States.mirror;
}
else if (Input.GetKeyDown(KeyCode.L))
{
myState = States.lock_0;
}
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_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_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_0;
}
}
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_mirror;
}
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.freedom;
}
else if (Input.GetKeyDown(KeyCode.R))
{
myState = States.cell_mirror;
}
}
void state_freedom()
{
text.text = "You are FREE!\n\n" +
"Press P to Play again";
if (Input.GetKeyDown(KeyCode.P))
{
myState = States.cell;
}
}
}
}
}
}
}
Okay everyone no need to worry! i figured it all out! it was just a bracket error and it was messing up the whole thing
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.