- Home /
The type or namespace name 'StateMachine' could not be found (are you missing a using directive or an assembly reference?)
I'm getting this issue in Unity even though I've already created the class StateMachine. Here are the scripts in question. The error appears regarding this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestEnemy : MonoBehaviour
{
[SerializeField]
private float turnAngle;
[SerializeField]
private float turnRange;
private float viewAngle;
private float viewDistance;
private float speed;
private float hunger;
private float hungerSpeed;
private float maxHealth;
private List<Enemy> predators;
private List<State> machineStates;
private StateMachine testMachine;
public float health;
// Start is called before the first frame update
void Start()
{
GenerateMachine();
testMachine.Start();
//testIdle = new IdleMovement("Idle Movement", null);
turnRange = 10f;
}
// Update is called once per frame
void Update()
{
turnAngle = Random.Range(-turnRange, turnRange) * Mathf.PerlinNoise(Time.deltaTime, 0);
testMachine.Update();
}
void GenerateMachine()
{
IdleMovement testIdle = new IdleMovement("Test Idle", null, gameObject.transform, turnAngle);
machineStates.Add(testIdle);
testMachine = new StateMachine(machineStates);
}
}
in the same folder, I have the following script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class StateMachine
{
public List<State> machineStates;
public State runningState;
public StateMachine(List<State> machineStates)
{
this.machineStates = machineStates;
}
public void Start()
{
StartingRunningState();
}
public void Update()
{
runningState.Update();
EvaluateNextState();
}
public void StartingRunningState()
{
foreach (State state in machineStates)
{
if (state.nodeStatus == NodeStatus.RUNNING)
{
runningState = state;
}
}
}
protected void EvaluateNextState()
{
foreach (State nextState in runningState.possibleStates)
{
if (nextState.nodeStatus == NodeStatus.SUCCESS && nextState.priority > runningState.priority)
{
runningState = nextState;
}
}
}
public void StateDebug()
{
string CurrentRunningState()
{
return runningState.nodeName;
}
string NextPossibleStates()
{
string nextPossibleStates = "";
for (int i = 0; i < runningState.possibleStates.Count; i++)
{
if (i == 0)
{
nextPossibleStates += runningState.possibleStates[i];
} else
{
nextPossibleStates += ", " + runningState.possibleStates[i];
}
}
return nextPossibleStates;
}
}
}
Please help I don't understand why I'm getting this issue :(
Are you getting any other compilation errors? An incomplete compilation of the code due to another error could cause it to not recognize a class. Also make sure that your StateMachine class is the only class in that file, and that the file is called StateMachine.cs. You can also try right clicking on the state machine file in your project explorer in Unity and clicking "Reimport", or even going to the Assets menu in Unity and clicking "Reimport All"
Just for a point of reference, when the class isn't derived from MonoBehaviour, it's not necessary for the class and script (*.cs file) to have the same name.
Nope. That was the only error that popped up
Answer by Glass_Egg_02 · Oct 29, 2021 at 11:03 PM
Thanks for the comments! For some reason it's working now. I did absolutely nothing but it started working after a few hours.
Your answer
Follow this Question
Related Questions
script does not update inspector? 3 Answers
Set default length for an array of elements of a custom class in inspector 0 Answers
UnityEngine.dll doesn't fix namespace error? 1 Answer
C# error - A namespace can only types and namespace declarations 2 Answers
NullReferenceException yet Debug.Log shows nothing is null? 1 Answer