[Help] Error Code CS0149 on Visual Studio and Error CS119 (Lesson 27)

Hello, as mentioned in the title, I am experiencing some issues with the coding written during lesson 27. I have just installed Visual Studio 2019, and I have tried to follow exactly the instructions, but it shows two error codes. On VS it mentions CS0149 and it says “method name expected”, on Unity instead it says “error cs0119: expression denotes a variable where a method group was expected”.

Here are the screens

here is the code for the two scripts, may I ask you what am I doing wrong?

ADVENTURE GAME SCRIPT

public class AdventureGame : MonoBehaviour {

[SerializeField] Text textComponent;
[SerializeField] State startingState;



string[] daysoftheweek = { "monday", "tuesday", "wednesday", "thursday", "saturday" };


State state;

// Use this for initialization
void Start () {
    state = startingState;
    textComponent.text = state.GetStateStory();
    Debug.Log(daysoftheweek[3]);

}

// Update is called once per frame
void Update () {
    ManageState();
}

private void ManageState()
{
    var nextStates = state.GetNextStates();
    if(Input.GetKeyDown(KeyCode.Alpha1))
    {
        state = nextStates(0);
    }
    else if (Input.GetKeyDown(KeyCode.Alpha2))
    {
        state = nextStates(1);
    }
    else if (Input.GetKeyDown(KeyCode.Alpha3))
    {

        state = nextStates(2);
    }
    textComponent.text = state.GetStateStory();
}

}

STATE SCRIPT

[CreateAssetMenu(menuName = “State”)]

public class State : ScriptableObject {

[TextArea(14, 10)] [SerializeField] string storyText;
[SerializeField] State[] nextStates;
public string GetStateStory()
{
    return storyText;
}




public State[] GetNextStates()
{
    return nextStates;
}

}

I apologize for the confusion and thank you in advance!!

Kind Regards,

Giuseppe

Hi Beppe,

Welcome to our forum. :slight_smile:

Parentheses are used for methods only. Is nextStates a method or something else?

Hi Nina,

thank you for the welcome and the prompt reply. I honestly have no clue :frowning:
Isn’t it a serialized field? I’ve followed the coding shown on the lecture, I might have added or missed something, but I don’t know what it could be… this is what it’s supposed to look like: https://github.com/CompleteUnityDeveloper/03-Text101/commit/7dcd402364692e312ff352572ac958beebca1d61

Methods do something. Arrays contain data.

Here is a method declaration:

int GetSum (int a, int b)
{
    return (a + b);
}

I hope the method is self-explanatory. If not: You pass on two integers, and it returns the sum as an integer, thus the int return type instead of void.

Now let’s call the method:

void Start ()
{
    int myNumber = GetSum(1, 2);
    Debug.Log("sum: " + myNumber);
}

See the parentheses? I passed on 1 and 2 as an argument to the GetSum method.

Now let’s take a look at an array:

string[] names = { "Ben", "Rick", "Michael", "Sam" };

void Start ()
{
    string name = names[2];
    Debug.Log("Hello, " + name);
}

See the difference between () and []?

In State.cs, you declare an array. Ignore [SerializeField], which is an attribute. The [] behind State is relevant. It tells the compiler that you want to create an array which will contain State objects.

[SerializeField] State[] nextStates;

And in AdventureGame, you get this array via:

var nextStates = state.GetNextStates();

I’m not a big fan of the var keyword but when you hover over the nextStates name, the compiler will tell you the type: State[]. Instead of using var, you could do this:

State[] nextStates = state.GetNextStates();

And then you access an object inside the nextStates array at index 0:

state = nextStates[0];

Now take a look at your code. Can you spot the problem?

Hi Nina, thank you again so much for the prompt reply and the explanation!

I have looked into it, and I think I’m understanding a bit more after your explanation, but I still can’t find the error I’ve made!

I’ll show you the code step by step, I honestly can’t see any mistake, and yet it says that a method name is expected.

As you said, in the State script, I declare the array:

public class State : ScriptableObject {

[TextArea(14, 10)] [SerializeField] string storyText;
[SerializeField] State[] nextStates;
public string GetStateStory()
{
    return storyText;
}

In the same script, if I’ve understood correctly, I’ve also created the method that gets the next state:

public State[] GetNextStates()
{
    return nextStates;
}

As you said, in the AdventureGame script, I call the array with the var function:

which is the same code I’ve written:

private void ManageState()
{
var nextStates = state.GetNextStates();
if(Input.GetKeyDown(KeyCode.Alpha1))
{
state = nextStates(0);
}

And yet, the error code remains, could it be that Visual Studio 2019 has some differences from the one used in the lecture, or do you think I have made a typo somewhere?

I apologize for the inexperience, and thank you again for the support.

Admittedly, it’s a bit difficult to see the difference between the round and the square brackets when you are not used to it. The following are parentheses. This is a method call. However, nextStates is not a method call, it’s an array which you want to access.

Solution
state = nextStates[0];

Hi Nina, I finally had the time to get back on the project and it’s working at last! Thank you so much for the help and your patience, it’s one thing to struggle a bit with the first steps in Visual Studio, but I was very silly not understanding the solution you offered me the first time! :sweat_smile:
Thank you again!

I was very silly not understanding the solution you offered me the first time!

As long as you understand the difference between a method call and an array now, there is nothing to worry about. :slight_smile:

Enjoy the course!

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

Privacy & Terms