IEnumerator ChangeSequece(TStateID next, StateInitParam initParam) { StateResult stateResult = null; if (currentState) { currentState.TermStart(); while (!currentState.IsFinished) { yield return(null); } stateResult = currentState.GetResult(); GameObject.Destroy(currentState.gameObject); currentState = null; } GameObject prefab = statePrefabs.Find(N => N.stateID.Equals(next)).statePrefab; GameObject gob = GameObject.Instantiate(prefab, this.transform); currentState = gob.GetComponent <StateBase>(); Debug.Log(currentState.GetType().ToString()); if (initParam != null) { Type[] types = null; object[] parameters = null; if (stateResult != null) { types = new Type[] { initParam.GetType(), stateResult.GetType() }; parameters = new object[] { initParam, stateResult }; } else { types = new Type[] { initParam.GetType() }; parameters = new object[] { initParam }; } var methodInfo = currentState.GetType().GetMethod("InitState", types); methodInfo?.Invoke(currentState, parameters); } currentState.PrepareStart(); while (!currentState.IsPrepared) { yield return(null); } }