{"record":{"id":"bd77f618fa2cc6d5","repo":"EllanJiang/GameFramework","slug":"fsm-is-running-can-not-start-again","errorCode":null,"errorMessage":"FSM is running, can not start again.","messagePattern":"FSM is running, can not start again\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Fsm/Fsm.cs","lineNumber":260,"sourceCode":"                }\n\n                m_Datas.Clear();\n            }\n\n            m_CurrentState = null;\n            m_CurrentStateTime = 0f;\n            m_IsDestroyed = true;\n        }\n\n        /// <summary>\n        /// 开始有限状态机。\n        /// </summary>\n        /// <typeparam name=\"TState\">要开始的有限状态机状态类型。</typeparam>\n        public void Start<TState>() where TState : FsmState<T>\n        {\n            if (IsRunning)\n            {\n                throw new GameFrameworkException(\"FSM is running, can not start again.\");\n            }\n\n            FsmState<T> state = GetState<TState>();\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), typeof(TState).FullName));\n            }\n\n            m_CurrentStateTime = 0f;\n            m_CurrentState = state;\n            m_CurrentState.OnEnter(this);\n        }\n\n        /// <summary>\n        /// 开始有限状态机。\n        /// </summary>\n        /// <param name=\"stateType\">要开始的有限状态机状态类型。</param>\n        public void Start(Type stateType)","sourceCodeStart":242,"sourceCodeEnd":278,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Fsm/Fsm.cs#L242-L278","documentation":"Fsm<T>.Start<TState>() transitions the FSM into its initial state. A state machine may only be started once; if IsRunning is already true, the guard at Fsm.cs:260 throws this exception instead of silently restarting the machine.","triggerScenarios":"Calling Start<TState>() twice on the same Fsm<T> instance — e.g. Start in both an init method and OnEnable, or Start called again after ChangeState/use without creating a fresh FSM.","commonSituations":"Unity components whose OnEnable runs multiple times (disable/enable cycles) re-calling Start; re-initializing gameplay logic without destroying and recreating the FSM via the FsmComponent; two systems each attempting to start a shared FSM.","solutions":["Call Start<TState>() only once per FSM lifetime; guard subsequent calls with if (!fsm.IsRunning) fsm.Start<TState>();","If a restart is intended, destroy/shutdown the existing FSM (return it to ReferencePool via the FsmComponent) and recreate it before starting.","Move the Start call out of OnEnable into a one-time initialization path (e.g. Start() or an explicit Init method)."],"exampleFix":"// before\nvoid OnEnable()\n{\n    m_Fsm.Start<IdleState>(); // throws if already running\n}\n// after\nvoid OnEnable()\n{\n    if (m_Fsm != null && !m_Fsm.IsRunning)\n    {\n        m_Fsm.Start<IdleState>();\n    }\n}","handlingStrategy":"type-guard","validationCode":"if (m_Fsm == null || m_Fsm.IsDestroyed) { RecreateFsm(); }\nif (!m_Fsm.IsRunning) { m_Fsm.Start<IdleState>(); }","typeGuard":"bool CanStart(Fsm<MyComponent> fsm) => fsm != null && !fsm.IsRunning;","tryCatchPattern":"try { m_Fsm.Start<IdleState>(); }\ncatch (GameFrameworkException ex) { Log.Warning(\"FSM already started: \" + ex.Message); }","preventionTips":["Call Start<TState>() exactly once, from a one-time init path — not OnEnable, which can run repeatedly.","Check IsRunning before any Start call in shared or re-entrant code paths.","To restart, destroy and recreate the FSM through the FsmComponent instead of calling Start again."],"tags":["csharp","fsm","invalid-state"],"backgroundTag":"invalid-state-transition","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"}