{"record":{"id":"30e6fa797fb53a95","repo":"EllanJiang/GameFramework","slug":"state-type-0-is-invalid","errorCode":null,"errorMessage":"State type '{0}' is invalid.","messagePattern":"State type '(.+?)' is invalid\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Fsm/Fsm.cs","lineNumber":292,"sourceCode":"        /// <summary>\n        /// 开始有限状态机。\n        /// </summary>\n        /// <param name=\"stateType\">要开始的有限状态机状态类型。</param>\n        public void Start(Type stateType)\n        {\n            if (IsRunning)\n            {\n                throw new GameFrameworkException(\"FSM is running, can not start again.\");\n            }\n\n            if (stateType == null)\n            {\n                throw new GameFrameworkException(\"State type is invalid.\");\n            }\n\n            if (!typeof(FsmState<T>).IsAssignableFrom(stateType))\n            {\n                throw new GameFrameworkException(Utility.Text.Format(\"State type '{0}' is invalid.\", stateType.FullName));\n            }\n\n            FsmState<T> state = GetState(stateType);\n            if (state == null)\n            {\n                throw new GameFrameworkException(Utility.Text.Format(\"FSM '{0}' can not start state '{1}' which is not exist.\", new TypeNamePair(typeof(T), Name), stateType.FullName));\n            }\n\n            m_CurrentStateTime = 0f;\n            m_CurrentState = state;\n            m_CurrentState.OnEnter(this);\n        }\n\n        /// <summary>\n        /// 是否存在有限状态机状态。\n        /// </summary>\n        /// <typeparam name=\"TState\">要检查的有限状态机状态类型。</typeparam>\n        /// <returns>是否存在有限状态机状态。</returns>","sourceCodeStart":274,"sourceCodeEnd":310,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Fsm/Fsm.cs#L274-L310","documentation":"Fsm.Start(Type) throws this when the given type does not derive from FsmState<T>, the required state base class for this FSM's owner type. Start only accepts valid state types of the matching owner type.","triggerScenarios":"Passing a Type that is not a subclass of FsmState<T> to Start(Type), e.g. a state registered for a different owner type FsmState<OtherOwner>, an unrelated class, or an interface.","commonSituations":"Mixing states from two FSMs with different owner types; using the wrong T generic parameter when creating the FSM; config-driven startup resolving a type name that points at the wrong class after a refactor.","solutions":["Make the state class derive from FsmState<T> with the same owner type T as the FSM","Use the strongly typed Start<TState>() overload so the compiler enforces the base class","Fix the owner type T used in Fsm.Create/AddFsm if states were written for a different owner"],"exampleFix":"// before\nfsm.Start(typeof(SomeClass)); // not an FsmState<T>\n// after\npublic class SomeClass : FsmState<FsmOwner> { }\nfsm.Start(typeof(SomeClass));","handlingStrategy":"type-guard","validationCode":"if (stateType == null || !typeof(FsmState<T>).IsAssignableFrom(stateType))\n    throw new ArgumentException(\"Type must derive from FsmState<T>\");\nfsm.Start(stateType);","typeGuard":"bool IsStateOfType<TOwner>(Type t) => t != null && typeof(FsmState<TOwner>).IsAssignableFrom(t);","tryCatchPattern":"try { fsm.Start(stateType); }\ncatch (GameFrameworkException ex) { Log.Error(\"State type {0} is not a valid FsmState<T>\", stateType?.FullName); }","preventionTips":["Derive every state from FsmState<T> with the FSM's owner type","Use generic Start<TState>() so the compiler enforces the base class","Avoid sharing state classes across FSMs with different owner types"],"tags":["unity","fsm","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}