I am getting errors in Unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hacker : MonoBehaviour {

// Use this for initialization
void Start ()
{
    ShowMainMenu("Hello Donald!");
    print("Hello" + "World");
}
void ShowMainMenu(string greeting)
{
    Terminal.ClearScreen();
    Terminal.WriteLine(greeting);
    Terminal.WriteLine("Which company's credit card database ");
    Terminal.WriteLine("would you like to hack into?");
    Terminal.WriteLine("Press 1 for Sony");
    Terminal.WriteLine("Press 2 for Nintendo");
    Terminal.WriteLine("Press 3 for Microsoft");
    Terminal.WriteLine("Make your selection:");
}
int level;

void OnUserInput(string input)
{
    if (input == "menu") //boolean true or false
    {
        ShowMainMenu("Hello, welcome back,");

    }
    else if (input == "1")
    { StartGame();
    }
    else if (input == "007")
    {
        Terminal.WriteLine("Please select a level Mr.Bond");
            }
    else
    {
        Terminal.WriteLine("Please choose a valid level");
    }
    void StartGame()
    {
        Terminal.WriteLine("You have chosen level" + level);
    }
    }

}

ERRORS
44,9 keyword void cannot be used in this context
44,22 unexpected symbol
49,0 unexpected symbol

Hello, it looks like you defined the function StartGame() inside of your OnUserInput() function. This will cause a compile error. To fix it, move the StartGame() function outside of the OnUserInput()'s curly brakets like this.

void OnUserInput(string input)
{
    if (input == "menu") //boolean true or false
    {
        ShowMainMenu("Hello, welcome back,");

    }
    else if (input == "1")
    { StartGame();
    }
    else if (input == "007")
    {
        Terminal.WriteLine("Please select a level Mr.Bond");
            }
    else
    {
        Terminal.WriteLine("Please choose a valid level");
    }
    
}

void StartGame()
{
    Terminal.WriteLine("You have chosen level" + level);
}

Just make sure all the code for StartGame() (including its curly brackets) are placed inside of the class curly brackets (public class Hacker).

2 Likes

Okay now it is saying unexpected symbol in void and else. On 10,8 and 39,8 respectively.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hacker : MonoBehaviour {
//Game state
int level;
enum screen
// Use this for initialization
void Start ()
{
ShowMainMenu(“Hello Donald!”);
print(“Hello” + “World”);
}
void ShowMainMenu(string greeting)
{
Terminal.ClearScreen();
Terminal.WriteLine(greeting);
Terminal.WriteLine("Which company’s credit card database ");
Terminal.WriteLine(“would you like to hack into?”);
Terminal.WriteLine(“Press 1 for Sony”);
Terminal.WriteLine(“Press 2 for Nintendo”);
Terminal.WriteLine(“Press 3 for Microsoft”);
Terminal.WriteLine(“Make your selection:”);
}

void OnUserInput(string input)
{
    if (input == "menu") //boolean true or false
    {
        ShowMainMenu("Hello, welcome back,");

    }
    else if (input == "1")
        level = 1;
    { StartGame();
    }
    else if (input == "007")
    {
        Terminal.WriteLine("Please select a level Mr.Bond");
            }
    else
    {
        Terminal.WriteLine("Please choose a valid level");
    }
    
    }
void StartGame()
{
    Terminal.WriteLine("You have chosen level" + level);
}

}

Quite often, any error after the 1st one, is “fallout”… errors that are only caused by the error which preceded them. So, rule #1 is: Always focus on the 1st error. Rule #2 is: decipher the error. If the error says, “10,8 error-type” what it’s telling you is that the location of the error is line 10, the 8th position from the left. Rule 3 is: fix the first error, and try to run the program again (unless you already understand the other errors, because they may only be caused by the 1st issue).

Hopefully, knowing this, anyone can start to solve these issues on their own or with Google’s help.

This case is an interesting one though, I see, as the REAL error, is on line 9… no “;” after your statement: enum screen … I think it should be:

enum screen;

White-space (tabs, spaces, returns) is ignored by the compiler, so what it saw in your code was the statement:

enum screen void start() { //method here
}

and that confused it, but it did know the word void didn’t belong there, so that’s why that’s where the error flag points.

1 Like

Okay thank you so much. I have been comparing side by side the lecture to my own and am getting errors on the “else” part now.

Would you just recommend me copy and pasting the lecture note section over so that I get no errors then customizing the code off of that? It is just very difficult for me to grasp all this code.

else is at 40,10. Does that mean line 10 or 40?

Close, but you’ve got them transposed. If the error says “40,10” the flag is pointing at line 40, 10th column.

Since Ben has each lesson checked-in on GitHub (and linked on Udemy) it’s certainly one way to go about it (as you describe, copying his code, then playing with it).

I’m making a wild guess that your new error is related to a bracket, The primary “punctuation marks” used by your C compiler are: brackets, semi-colons, and commas. Any whitespace is equivalent (tabs, spaces, returns) and isn’t really a form of punctuation, merely a way to separate the “words”. With this in-mind, you need to have an understanding of what the punctuation means.

Semicolon - used to demark the end of a statement (i.e. a sentence to the compiler).
Brackets - Must always be used in matched pairs, used to demark the contents for certain types.

For example, a properly formatted IF statement uses () to encapsulate the expression to test, and then uses {} to encapsulate the (group of) statement(s) to processes when the evaluation is true.

I"d guess you managed to get one of your ELSEs inside the {} of an IF…

1 Like

Privacy & Terms