The part at 5:50 in the video is rather confusing

The switching back and forth from the UIFader to AreaEntrance and AreaExit scripts and back again into the UIFader to make it a singleton is rather confusing to follow.

It would be much better to first setup the UIFader to be a singleton and then go through the two other ones to add its invocation.

Actually, I would have fully expected the calls to FadeToBlack() and FadeToClear() to be done as a challenge…

Alright, there is a challenge later on, but I wonder why not just Invoke() it with a delay?

        private void OnTriggerEnter2D(Collider2D other)
        {
            if (other.gameObject.GetComponent<PlayerController>())
            {
                UIFade.Instance.FadeToBlack();
                SceneManagement.Instance.SetTransisionName(sceneTransitionName);
                Invoke(nameof(LoadNextScene), sceneTransitionDelay);
            }
        }

        private void LoadNextScene()
        {
            SceneManager.LoadScene(sceneToLoad);
        }

I do find that it (seemingly?) fades to clear faster than to black, though…

I think the switching you are referring to is just where Stephen opens the files ready to be used but doesnt actually enter anything at that point as we still need to set up the UIFade as a singleton.
I do agree its easier to follow if you finish the class you are working in before opening other classes.

I wonder if its a teaching point avoiding the invoke but as you say that produces an result where it appears to clear faster than fading to black.
I think only @Stephen_Hubbard (Sorry for keep pinging you bud!) can answer as its a matter of choice and i cant remember exactly how we did it in the RPG course.

Incidentally i went a completely different way about this from the challenge and invoking and it still works fine.
Not sure if there is anything wrong with doing it this way (I had a crash but i think it was unrelated)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class AreaExit : MonoBehaviour
{
    [SerializeField] private string sceneToLoad;
    [SerializeField] private string sceneTransitionName;

    private float waitToLoadTime = 1f;


    private void OnTriggerEnter2D(Collider2D other) 
    {
       if(other.gameObject.GetComponent<PlayerController>()) 
       {
            StartCoroutine(LoadSceneRoutine());    
       }
    }

    private IEnumerator LoadSceneRoutine()
    {
        UIFade.Instance.FadeToBlack();
        yield return new WaitForSeconds(waitToLoadTime);
        SceneManager.LoadScene(sceneToLoad);
        SceneManagement.Instance.SetTransitionName(sceneTransitionName);
    }
}

This simply starts the coroutine from the trigger, Does the fade, Waits a second and loads the scene which then triggers the clearfade.

It just seemed a whole lot simpler this way

Well, it’s probably cleaner in a way to encapsulate all that is required for the scene transitioning into its own method that just gets called from within the OnTriggerEnter2D() callback method. Doing it directly as a Coroutine (thus being able to simply yield return new WaitForSeconds() in this case saves an extra method that’s just there for the delayed call to load the other scene…

One thing though… I would definitely place the LoadScene() call last into it, same as I wouldn’t have any code past a Destroy() instruction…

1 Like

That’s what I was referring to, indeed.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms