private void DoStateChange() { if (_currentState != null) { _currentState.ExitState(); _currentState.DeregisterErrorHandler(); } _previousState = _currentState; _currentState = _nextState; _currentState.RegisterErrorHandler(ErrorOccurred); _currentState.EnterState(); _nextState = null; if (onStateChanged != null && _previousState != null) { onStateChanged(_previousState, _currentState); } }