Hello guys,
I am having trouble on Section 3 Lecture 31. I’ve basically entered all the coding in Monodevelop to set up the button inputs. S to view Sheets and R to Return to roaming your cell. I saved all that and went into Unity to hit the play button for play mode and I tried hitting S but nothing happens… I also can’t seem to get rid of the ‘Hello World’ text to.
1 Like
Hello @lifeofagoodkid, how are you?
Would you mind sharing your code please?
Hi @Joao_Dalvi sure do.
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.sheets_0) {
state_sheets_0();
}
}
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 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;
}
}
1 Like
humm…
Have you added this script to a gameobject inside the scene and then dragged the main text gameobject (the one that is displaying “Hello World”) to the script inspector? the way this script is, it should display the text.text inside state_cell() right after you press play, and don’t seems to be just a link problem since it would return a null reference error
@Joao_Dalvi I havant tried yet, but i will do and then get back to you
@Joao_Dalvi and how would you do all that? Do I need to be in monodeveloper to do that or just unity?
Just the unity,
Drag the script from your projecy asset to the inspector of the GameObject with the text, and then drag the text component that is inside the inspector to the text public variable within the same gameoject (everything done inside the same inspector window)
Having yet another problem
I opened up unity and now it’s saying that I have a parsing error. Here’s the message it reads: Assets/TextController.cs(45,9): error CS8025: Parsing error. I opened up Mono develop and it doesn’t highlight the areas in red that I need to fix…
1 Like
can you paste your current 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);
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, 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 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;
}
}
Was basically the same code as before and no modifications
seems that you are missing the last curly brace that closes the monobehaviour
Ok so after line 43 which: " myState = States.cell; " is inputted in the program, lines 44 and 45 has two curly braces. So there should be 3 curly braces in total, so another one on line 46?
Yessir
You are missing the brace that "closes " the class
Is it suppose to be like this:
}
}
Now its sorted, but now another issue has arise
I saved everything in monodevelop and I when I got to Unity and press play for playmode, it just shows a blue screen and a small white box in the center of the screen and no text.
can you send a picture of both the game window (after play) and the scene window (before play) too?