Creating New Method @ 1:14

When I create the new method at 1:14 in the video I am getting a ‘ManageState’: member name cannot be the same as their enclosing type error.

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

public class ManageState : MonoBehaviour
{

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

    State state;

    // Start is called before the first frame update
    void Start()
    {
        state = startingState;
        textComponent.text = state.GetStateStory();
    }

    // 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();
    }
}

Hi Don, welcome to the community :slight_smile:

Can you copy/paste your full script and apply code formatting please. Judging by your error message it looks as if you may have named this class ManageState.

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Hope this helps :slight_smile:


See also;

Hi Don,

Thank you for updating your post, I can see now that you have indeed named your class ManageState and you are then trying to name a method with the same name, this isn’t allowed.

Consider renaming the class to AdventureGame as per the lecture;

public class AdventureGame : MonoBehaviour
{
    // your code
}

…and also rename the file itself to AdventureGame.cs, the method will then not error regarding your choice of name.

Hope this helps :slight_smile:

1 Like

I can’t believe I overlooked that. Thanks for the quick reply and the help.

1 Like

No problem at all, glad you can move forward again :slight_smile:


See also;

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

Privacy & Terms