Home > OS >  Having trouble with an Error CS1031: Type expected
Having trouble with an Error CS1031: Type expected

Time:12-19

Having trouble. Can't find where I messed up. If anyone can spot the issue I'd appreciate it greatly. The error is: Error CS1031: Type expected I've tried running the Unity Debug thing on the script with no luck. I just genuinely don't see an issue.

'''
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class AiStateMachine : 
{
    public AiState[] states;
    public AiAgent agent;
    public AiStateId currentState; 

    public AiStateMachine(AiAgent agent)
    {
        this.agent = agent;
        int numStates = System.Enum.Getnames(typeof(AiStateId)).Length;
        states = new AiState[numStates];

    }

    public void RegisterState(AiState state)
    {
        int index = (int)state.GetId();
        states[index] = state;
    }

    public AIStateMachine GetState(AiStateId stateId) 
    {
        int index = (int)stateId;
        return states[index];
    }

    public void Update() 
    {
        GetState(currentState)?.Update(agent);

    }

    public void ChangeState(AiStateId newState)
    {
        GetState(currentState)?.Exit(agent);
        currentState = newState;
        GetState(currentState)?.Enter(agent); 
    }
}
'''

CodePudding user response:

You have a dangling colon (:) after the class name. Since you aren't inheriting any class, you shouldn't have the colon there:

public class AiStateMachine 
{
    // ":" removed here ----^

CodePudding user response:

While it isn't necessarily helpful in this particular case I want to mention that if you google the error code there is usually some info from Microsoft to help some. Here's the one for this error:

https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs1031

In this case though it looks like you have a class that is expecting to derive from something by having a colon at the end of the class declaration without any info after it:

public class AiStateMachine : 

Try removing the colon or adding whatever the class should inherit from. This page also has some great info to brush up on inheritance if needed:

https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/tutorials/inheritance

  • Related