Can't get a simple script to work

I sat down for about 3 hours and made a simple game, and then got a few bugs and couldn’t figure out how to fix them. I am a beginner to unity and was just trying to make a game to learn(Let me just say I haven’t been so frustrated in quite a while). Here is my script:

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

public class TextController : MonoBehaviour {

public enum States {cell, mirror, mirror_1, bed_0, cell_mb, cell_m, cell_b, bed_1, lock_1, freedom, lose};
public States myState;

public Text text;

// 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.bed_1 {state_bed_1();}
else if (myState == States.bed_0 {state_bed_0();}
else if (myState == States.cell_m {state_cell_m();}
else if (myState == States.cell_b {state_cell_b();}
else if (myState == States.lock_1 {state_lock_1();}
else if (myState == States.cell_mb {state_cell_mb();}
else if (myState == States.freedom {state_freedom();}
else if (myState == States.lose {state_lose();}
else if (myState == States.mirror_1 {state_mirror_1();
}
}
void state_cell () {
text.text = "You have been framed for a crime and are now in prison, you need to find a way to " +
"escape. Whatever you can do you must try. There is a bed to " +
“your right, a small mirror to your left, and the door is locked from the outside. \n\n” +
“Press B to view Bed and Press M to view Mirror”;
if(Input.GetKeyDown(KeyCode.B)) {
myState = States.bed_0;
if(Input.GetKeyDown(KeyCode.M)) {
myState = States.mirror;
}
}
}
void state_bed_0 () {
text.text = "These beds look terrible, I can’t believe someone can sleep in here. You look through the " +
“bed and find a bobby pin, that could be useful you think. \n\n” +
“Press R to return to your cell”;
if(Input.GetKeyDown(KeyCode.R)) {
myState = States.cell_b;
}
}
void state_bed_1 () {
text.text = "These beds look terrible, I can’t believe someone can sleep in here. You look through the " +
“bed and find a bobby pin, that could be useful you think. \n\n” +
“Press R to return to your cell”;
if(Input.GetKeyDown(KeyCode.R)) {
myState = States.cell_mb;
}
}
void state_mirror () {
text.text = "There is a small mirror on the ground, you pick it up and immediately think " +
“of an idea. \n\n” +
“Press R to return to your cell.”;
if(Input.GetKeyDown(KeyCode.R)) {
myState = States.cell_m;
}
}
void state_cell_m () {
text.text = "There is a small mirror on the ground, you pick it up and immediately think " +
“of an idea. \n\n” +
“Press B to view bed.”;
if(Input.GetKeyDown(KeyCode.B)) {
myState = States.bed_1;
}
}
void state_cell_b () {
text.text = “You have a bobby pin. \n\n” +
“Press M to view Mirror.”;
if(Input.GetKeyDown(KeyCode.M)) {
myState = States.mirror_1;
}
}
void state_mirror_1 () {
text.text = "There is a small mirror on the ground, you pick it up and immediately think " +
“of an idea. \n\n” +
“Press R to return to your cell.”;
if(Input.GetKeyDown(KeyCode.R)) {
myState = States.cell_bm;
}
}
void state_cell () {
text.text = “You have all of the items needed to escape. \n\n” +
“Press E to escape or Press P to stay.”;
if(Input.GetKeyDown(KeyCode.E)) {
myState = States.freedom;
if(Input.GetKeyDown(KeyCode.P)) {
myState = States.lose;
}
}
}
void state_freedom () {
text.text = “YOU WIN!!!”;
}
void state_lose () {
text.text = “YOU LOSE :(”;
}
}

I am getting a few errors on line 20-22.

Please edit your post and add code tags.

1 Like

Looks like some missing () and {} also your if statements have the close brackets in the wrong place

Generally when coding you want to iteratively develop. Write a few lines, flip back to Unity to make sure you have no compiler errors, then return to coding. This is especially important when you are beginning, as compiler errors will guide you to a much deeper understanding of the software engineering process.

3 Likes

also use code tags, and post what your errors are. No one wants to read a wall of un indented text, while also not even knowing what to look for.

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

public class TextController : MonoBehaviour {

public enum States {cell, mirror, mirror_1, bed_0, cell_mb, cell_m, cell_b, bed_1, lock_1, freedom, lose};
public States myState;

    public Text text;

    // 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.bed_1         {state_bed_1();}
        else if (myState == States.bed_0         {state_bed_0();}
        else if (myState == States.cell_m         {state_cell_m();}
        else if (myState == States.cell_b         {state_cell_b();}
        else if (myState == States.lock_1         {state_lock_1();}
        else if (myState == States.cell_mb         {state_cell_mb();}
        else if (myState == States.freedom         {state_freedom();}
        else if (myState == States.lose            {state_lose();}
        else if (myState == States.mirror_1     {state_mirror_1();
    }
}
    void state_cell () {
                    text.text = "You have been framed for a crime and are now in prison, you need to find a way to " +
                    "escape. Whatever you can do you must try. There is a bed to " +
                    "your right, a small mirror to your left, and the door is locked from the outside. \n\n" +
                    "Press B to view Bed and Press M to view Mirror";
        if(Input.GetKeyDown(KeyCode.B)) {
            myState = States.bed_0;
        if(Input.GetKeyDown(KeyCode.M)) {
            myState = States.mirror;
        }
    }
}
    void state_bed_0 () {
        text.text = "These beds look terrible, I can't believe someone can sleep in here. You look through the " +
            "bed and find a bobby pin, that could be useful you think. \n\n" +
                "Press R to return to your cell";
        if(Input.GetKeyDown(KeyCode.R)) {
            myState = States.cell_b;
        }
    }
    void state_bed_1 () {
            text.text = "These beds look terrible, I can't believe someone can sleep in here. You look through the " +
                "bed and find a bobby pin, that could be useful you think. \n\n" +
                    "Press R to return to your cell";
        if(Input.GetKeyDown(KeyCode.R)) {
            myState = States.cell_mb;
        }
    }
        void state_mirror () {
            text.text = "There is a small mirror on the ground, you pick it up and immediately think " +
                        "of an idea. \n\n" +
                        "Press R to return to your cell.";
            if(Input.GetKeyDown(KeyCode.R)) {
                myState = States.cell_m;
        }
    }
        void state_cell_m () {
            text.text = "There is a small mirror on the ground, you pick it up and immediately think " +
                "of an idea. \n\n" +
                    "Press B to view bed.";
            if(Input.GetKeyDown(KeyCode.B)) {
                myState = States.bed_1;
        }
    }
        void state_cell_b () {
            text.text = "You have a bobby pin. \n\n" +
                        "Press M to view Mirror.";
            if(Input.GetKeyDown(KeyCode.M)) {
                myState = States.mirror_1;
        }
    }
        void state_mirror_1 () {
            text.text = "There is a small mirror on the ground, you pick it up and immediately think " +
                "of an idea. \n\n" +
                    "Press R to return to your cell.";
            if(Input.GetKeyDown(KeyCode.R)) {
                myState = States.cell_bm;
        }
    }
        void state_cell () {
            text.text = "You have all of the items needed to escape. \n\n" +
                        "Press E to escape or Press P to stay.";
            if(Input.GetKeyDown(KeyCode.E)) {
                myState = States.freedom;
            if(Input.GetKeyDown(KeyCode.P)) {
                myState = States.lose;
            }
        }
    }
        void state_freedom () {
            text.text = "YOU WIN!!!";
    }
        void state_lose () {
            text.text = "YOU LOSE :(";
    }
}

Check you full code…
There are so many () and {} false/missing

All of your States are false…

void state_cell () {
            text.text = "You have all of the items needed to escape. \n\n" +
                        "Press E to escape or Press P to stay.";
            if(Input.GetKeyDown(KeyCode.E)) {
                myState = States.freedom;
           } // you have to close your if
            if(Input.GetKeyDown(KeyCode.P)) {
                myState = States.lose;
            }
        }

And your statement check misses in each line the closing brackets )

if (myState == States.cell) // closing bracket          
  {state_cell();}
 else if (myState == States.mirror )    // closing Brackets
{state_mirror();}