DontDestroyOnLoad Problem

Hi guys,

I created a canvas that changes from white to black then to white again between changing scenes and it is working fine at first. Then I created a prefab to call my player and the UI canvas that I created as shown below as I move to a new scene. I also have use DontDestroyOnLoad in my player and Ui canvas scripts. Instead of using my UI canvas and Player, I replaced them with EssentialsLoader prefab and have made sure that I assigned my player and UI canvas to the EssentialsLoader prefab. When I try in play mode, only my player shows up under the DontDestroyOnLoad in the hierarchy tab and the UI canvas does not show. How do i fix this?

public class EssentialsLoader : MonoBehaviour
{
    public GameObject UIScreen;
    public GameObject player;

    // Start is called before the first frame update
    void Start()
    {
        if (UIFade.instance == null)
        {
            UIFade.instance = Instantiate(UIScreen).GetComponent<UIFade>();
        }

        if (PlayerController.instance == null)
        {
            PlayerController.instance = Instantiate(player).GetComponent<PlayerController>();
        }
       
    }

    // Update is called once per frame
    void Update()
    {
       
    }
}

As far as I’m aware you should never check if a component is null and should instead evaluate it as a boolean, i.e.

        if (!UIFade.instance)
        {
            UIFade.instance = Instantiate(UIScreen).GetComponent<UIFade>();
        }

This is due to Unity implementing custom behavior on the == operator. I don’t think that’s the source of your problem, but it’s worth checking if it is.