Error cs1031 требуется тип

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

Asked
1 year, 1 month ago

Viewed
1k times

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

Michael Roy's user avatar

Michael Roy

1761 gold badge1 silver badge7 bronze badges

asked Dec 19, 2021 at 13:37

Gunther Akers's user avatar

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 ----^

answered Dec 19, 2021 at 13:40

Mureinik's user avatar

MureinikMureinik

290k52 gold badges300 silver badges337 bronze badges

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://learn.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://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/tutorials/inheritance

answered Dec 19, 2021 at 13:46

Trite's user avatar

TriteTrite

1063 bronze badges

  • Remove From My Forums
  • Question

  • In the below code how can I resolve type expected error cs:1031

    publicclassPublicAndDeclare

    {

    [System.Runtime.InteropServices.

    DllImport(«Plantcon.dll»,
    EntryPoint=
    «TerminatePlantCon»,ExactSpelling=true,
    CharSet=System.Runtime.InteropServices.
    CharSet.Ansi,SetLastError=true)]

       }

    publicstaticexternlongInitPlantCon();

Answers

  • The next fragment seems to work:

    public
    class
    PublicAndDeclare
    {
          
    [System.Runtime.InteropServices.
    DllImport(«Plantcon.dll»,

                 
    EntryPoint =
    «TerminatePlantCon»,

                 
    ExactSpelling =
    true,

                 
    CharSet = System.Runtime.InteropServices.
    CharSet.Ansi,

                 
    SetLastError =
    true)]
          
    public
    static
    extern
    long InitPlantCon();
    }

    In which line the error is reported?

    • Edited by

      Tuesday, April 9, 2013 6:14 AM

    • Marked as answer by
      Bob Shen
      Tuesday, April 23, 2013 10:42 AM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
        static void Main(string[] args)
        {
 
            int[,] a1 =
                {
                    {3, 7, 9, 2, 4, 6, 8, 1, 5},
                    {5, 2, 8, 1, 3, 7, 6, 9, 4},
                    {1, 6, 4, 5, 8, 9, 2, 7, 3},
                    {8, 4, 7, 0, 5, 2, 1, 6, 9},
                    {6, 1, 3, 8, 9, 4, 5, 2, 7},
                    {2, 9, 5, 6, 7, 1, 3, 4, 8},
                    {4, 3, 1, 9, 6, 8, 7, 1, 2},
                    {7, 5, 2, 4, 1, 3, 9, 8, 6},
                    {9, 8, 6, 7, 2, 5, 4, 3, 1}
                };
            if (SudocuValid(a1))
                Console.WriteLine("Судоку нарисовано Васей правильно ");
            else
                Console.WriteLine("Судоку нарисовано Васей не правильно ");
 
            int[,] a =
                {
                    {3, 7, 9, 2, 4, 6, 8, 1, 5},
                    {5, 2, 8, 1, 3, 7, 6, 9, 4},
                    {1, 6, 4, 5, 8, 9, 2, 7, 3},
                    {8, 4, 7, 3, 5, 2, 1, 6, 9},
                    {6, 1, 3, 8, 9, 4, 5, 2, 7},
                    {2, 9, 5, 6, 7, 1, 3, 4, 8},
                    {4, 3, 1, 9, 6, 8, 7, 5, 2},
                    {7, 5, 2, 4, 1, 3, 9, 8, 6},
                    {9, 8, 6, 7, 2, 5, 4, 3, 1}
                };
            if (SudocuValid(a))
                Console.WriteLine("Судоку нарисовано Петей правильно ");
            else
                Console.WriteLine("Судоку нарисовано Петей не правильно ");
    }

Понравилась статья? Поделить с друзьями:
  • Error cs1024 требуется директива препроцессора
  • Error cs1024 preprocessor directive expected
  • Error cs1022 требуется определение типа или пространства имен либо признак конца файла
  • Error cs1022 ожидалось определение типа или пространства имен либо признак конца файла
  • Error cs1022 type or namespace definition or end of file expected