IEnumerator StateTransition(State nextState) { // Toggling activated objects from states if (currentState != null) { string[] theTags = currentState.GetActivatedTags(); foreach (string thisTag in theTags) { ToggleObjByName(thisTag); } } string[] nextTags = nextState.GetActivatedTags(); foreach (string nextTag in nextTags) { ToggleObjByName(nextTag); } // Updating state currentState = nextState; // Fading old state out textBGFade.FadeOut(); textFade.FadeOut(); buttonHolderFade.FadeOut(); // Transition Audio bool[] clipBools = nextState.GetClipBools(); bool rainOn = clipBools[0]; bool rainMuffled = clipBools[1]; bool musicOn = clipBools[2]; bool musicMuffled = clipBools[3]; bool waterDripOn = clipBools[4]; bool heartBeatOn = clipBools[5]; rainFilter.enabled = rainMuffled; rainSource.mute = !rainOn; rainFilter.enabled = rainMuffled; musicSource.mute = !musicOn; musicFilter.enabled = musicMuffled; waterDripSource.mute = !waterDripOn; heartBeatSource.mute = !heartBeatOn; // Updating background image Sprite nextSprite = nextState.GetBackgroundImage(); if (nextSprite != null) { BackgroundFade(nextSprite); yield return(new WaitForSeconds(0.1f)); while (theBGImage2imageFade.CheckFading()) { yield return(new WaitForSeconds(0.1f)); } } yield return(new WaitForSeconds(0.1f)); while (textBGFade.CheckFading() || textFade.CheckFading() || buttonHolderFade.CheckFading()) { yield return(new WaitForSeconds(0.1f)); } textComponent.text = nextState.GetStateStory(); // Adding new buttons var nextStates = nextState.GetNextStates(); var nextStatesNames = nextState.GetNextStatesNames(); var index = 0; foreach (State child in nextStates) { GameObject newButton = Instantiate(theButtonPrefab); newButton.transform.SetParent(buttonHolder.transform, false); string nextStateName = nextStatesNames[index]; newButton.GetComponentInChildren <TextMeshProUGUI>().text = nextStateName; newButton.GetComponentInChildren <Button>().onClick.AddListener(() => { theGame.NextState(child); }); index++; } // Fading in buttons float delayTime = 1f; if (GetCurrentStateName() == startingState.name) { delayTime = startDelay; } StartCoroutine(ButtonsFadeIn()); textBGFade.FadeIn(); textFade.FadeIn(); buttonHolderFade.FadeIn(); }