I Have A Problem In DontDestoyInLoad

I Was Using

private void Awake()
    {
     
        DontDestroyOnLoadgameObject);
    }

But I Was Getting Errors about the root component thing
so I used this instead

private void Awake()
    {
     
        DontDestroyOnLoad(transform.root.gameObject);
    }

When A Function Is Called, About

 public void LoadNextLevelFunction()
    {
        FindObjectOfType<LevelFaderScript>().FadeToLevel(SceneManager.GetActiveScene().buildIndex + 1);
    }

I Get The Canvas (Where The Script Is Sitting) in the dontdestroyonload and the transition doesn’t happen
How Can I Fix This

That’s weird. I just have to do: DontDestroyOnLoad(new GameObject());

May I see the full script?

This work:

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

public class DontDestroy : MonoBehaviour
{
    public GameObject gameobject;
    public int BuildIndex;

    void Awake ()
    {
        DontDestroyOnLoad(gameobject);
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.L))
        {
            SceneManager.LoadScene(BuildIndex);
        }
    }
}

didn’t work the child objects didnt follow the dontdestroyonload

using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelFaderScript : MonoBehaviour
{
   
    private int levelToLoad;
    public Animator animator;
    public ScenesClass[] scenes;

    private void Awake()
    {

        DontDestroyOnLoad(gameObject);

    }
    public void FadeToLevel(int levelIndex)
    {
        levelToLoad = levelIndex;
        animator.SetTrigger("FadeOut");
    }

    public void LevelFadeOut()
    {  
     
        SceneManager.LoadScene(levelToLoad);
        Time.timeScale = 1f;
    }

    public void FadeToMainMenu()
    {
        animator.SetTrigger("FadeOutMainMenu");
    }

    public void LevelFadeOutToMainMenu()
    {
        SceneManager.LoadScene(0);
    }

    public void LevelToAppyFadeIn()
    {
        animator.SetTrigger("FadeIn");
    }

    public void LevelToAppyFadeInMainMenu()
    {
        animator.SetTrigger("FadeInMenu");
    }
}

dont focus on everyfunction that have a mainmenu in it