The local function is not declared cs8321 [SOLVED]

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

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(); }
            else if (myState == States.sheets_1) { State_sheets_0(); }
            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.freedom) { State_freedom(); }

            
        }

    }
    private void State_freedom()
    {
        throw new NotImplementedException();
    }

    private void State_cell_mirror()
    {
        throw new NotImplementedException();
    }

    private void State_mirror()
    {
        NewMethod1();
    }

    private static void NewMethod1()
    {
        NewMethod();
    }

    private static void NewMethod()
    {
        throw new NotImplementedException();
    }

    private void State_lock_1()
    {
        throw new NotImplementedException();
    }

    private void State_lock()
    {
        throw new NotImplementedException();
    }

    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_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 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.freedom; }
             else if (Input.GetKeyDown(KeyCode.R)) { myState = States.cell_mirror; }


    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_mirror()
void State_lock_1()
Is where i am getting a problem i can not seem to declare it so i think it doesn’t no its there

Thank you

Hi,

You have missing closing brackets.

State_lock_0 and then you have placed State_mirror instead another method, State_lock_1

The error message is because this version of C# doesn’t support local methods, e.g
a method within another method.

The cause of the error is the braces, need to.pay close attention to these. If there is an opening bracket or brace it must have a corresponding closing one.

You may find, until you become mord familiar, that using a block style rather than a trailing style may help you find missing braces, e.g.

void Start()
{
    // ...
}

instead of;

void Start() {

    // ...
}

Give the block style a try and also be aware of the indenting, the tidier it is, the easier it is to find problems.


See also;

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(); }
            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.freedom) { State_freedom(); }


        

    }

    private void State_lock_1()
    {
        throw new NotImplementedException();
    }

    private void State_freedom()
    {
        throw new NotImplementedException();
    }

    private void State_cell_mirror()
    {
        throw new NotImplementedException();
    }

    private void State_mirror()
    {
        throw new NotImplementedException();
    }



    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_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 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()** DEBUG
    {
        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; }



   }

} 

sorry i am new if my code isn’t being uploaded correctly
i tried to tidy the brackets up a bit as i noticed there was a lot i was not even using
am still getting the problem not sure what you was saying on trying to resolve it

THANK YOU

Hi Conna,

The suggestion for the block style braces and brackets was more to help you, when you see them lining up it is just a little easier on the eye for you to spot things.

Since you’ve made those changes it is a lot easier for me to read also - bonus! :slight_smile:

With regards to uploading your code into the forum, you can just copy and paste it in, but if you also apply the code formatting characters, before and after the code itself, the forum will display it as code and will highlight syntax and so on. One of the benefits of doing so is that it allows those who offer to help you the ability to just copy/paste chunks of your code back to you, where-as with a screenshot for example, they would have to type it all out - this can be a bit of a turn off for some and you may get less responses. I provided you with the link above to the Forum User Guide regarding the code formatting so that should help, and I’ve done it for you on the above.

The following is your code which you have posted above, with also the top part from you original post, the only changes I have made are;

  • removed additional / unnecessary spacing
  • tidied up some indenting here and there
  • removed the asterisks and DEBUG wording from the method State_lock_1 (at the bottom)

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

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(); }
		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.freedom) { State_freedom(); }
	}

	private void State_lock_1()
	{
		throw new NotImplementedException();
	}

	private void State_freedom()
	{
		throw new NotImplementedException();
	}

	private void State_cell_mirror()
	{
		throw new NotImplementedException();
	}

	private void State_mirror()
	{
		throw new NotImplementedException();
	}

	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_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 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.freedom; }
		else if (Input.GetKeyDown(KeyCode.R)) { myState = States.cell_mirror; }
	}
}

So, if you make the same changes, or just copy/paste the above over the top of your script and then run your game, if you do still get an error, what is it and which line number does it point to? If you are not certainly, please take a screenshot of the error message from the console, or copy/paste it. :slight_smile:


Updated Thu May 10 2018 10:16

I think I have spotted your problem. You have created the new and complete method State_lock_1 but if you scroll up in your code a little bit, you will see that you have already defined it;

private void State_lock_1()
{
	throw new NotImplementedException();
}

You can’t have two methods in the same script with the same signature, so, now that you have implemented your complted version, delete the above duplicate and that should remove your error :slight_smile:

1 Like

that has worked thank you very much will try upload code instead of pic it seems whenever i copy and paste the code it seems to come out like a pic when i am really just copy and pasting

Thanks again for all your help and advice much appreciated

1 Like

You’re more than welcome Conna, I am glad you can move forward with the rest of the section again :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms