Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

I Have A Problem In DontDestoyInLoad

Discussion in 'Scripting' started by RyanGamingKob, Nov 9, 2019.

  1. RyanGamingKob

    RyanGamingKob

    Joined:
    Oct 27, 2019
    Posts:
    18
    I Was Using
    Code (CSharp):
    1. private void Awake()
    2.     {
    3.      
    4.         DontDestroyOnLoadgameObject);
    5.     }
    But I Was Getting Errors about the root component thing
    so I used this instead
    Code (CSharp):
    1. private void Awake()
    2.     {
    3.      
    4.         DontDestroyOnLoad(transform.root.gameObject);
    5.     }
    When A Function Is Called, About
    Code (CSharp):
    1.  public void LoadNextLevelFunction()
    2.     {
    3.         FindObjectOfType<LevelFaderScript>().FadeToLevel(SceneManager.GetActiveScene().buildIndex + 1);
    4.     }
    I Get The Canvas (Where The Script Is Sitting) in the dontdestroyonload and the transition doesn't happen
    How Can I Fix This
     
  2. Deleted User

    Deleted User

    Guest

    That's weird. I just have to do: DontDestroyOnLoad(new GameObject());
     
  3. Deleted User

    Deleted User

    Guest

    May I see the full script?
     
  4. Deleted User

    Deleted User

    Guest

    This work:
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine.SceneManagement;
    3. using UnityEngine;
    4.  
    5. public class DontDestroy : MonoBehaviour
    6. {
    7.     public GameObject gameobject;
    8.     public int BuildIndex;
    9.  
    10.     void Awake ()
    11.     {
    12.         DontDestroyOnLoad(gameobject);
    13.     }
    14.     void Update()
    15.     {
    16.         if (Input.GetKeyDown(KeyCode.L))
    17.         {
    18.             SceneManager.LoadScene(BuildIndex);
    19.         }
    20.     }
    21. }
     
  5. RyanGamingKob

    RyanGamingKob

    Joined:
    Oct 27, 2019
    Posts:
    18
    didn't work the child objects didnt follow the dontdestroyonload
     
  6. RyanGamingKob

    RyanGamingKob

    Joined:
    Oct 27, 2019
    Posts:
    18
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. public class LevelFaderScript : MonoBehaviour
    4. {
    5.    
    6.     private int levelToLoad;
    7.     public Animator animator;
    8.     public ScenesClass[] scenes;
    9.  
    10.     private void Awake()
    11.     {
    12.  
    13.         DontDestroyOnLoad(gameObject);
    14.  
    15.     }
    16.     public void FadeToLevel(int levelIndex)
    17.     {
    18.         levelToLoad = levelIndex;
    19.         animator.SetTrigger("FadeOut");
    20.     }
    21.  
    22.     public void LevelFadeOut()
    23.     {  
    24.      
    25.         SceneManager.LoadScene(levelToLoad);
    26.         Time.timeScale = 1f;
    27.     }
    28.  
    29.     public void FadeToMainMenu()
    30.     {
    31.         animator.SetTrigger("FadeOutMainMenu");
    32.     }
    33.  
    34.     public void LevelFadeOutToMainMenu()
    35.     {
    36.         SceneManager.LoadScene(0);
    37.     }
    38.  
    39.     public void LevelToAppyFadeIn()
    40.     {
    41.         animator.SetTrigger("FadeIn");
    42.     }
    43.  
    44.     public void LevelToAppyFadeInMainMenu()
    45.     {
    46.         animator.SetTrigger("FadeInMenu");
    47.     }
    48. }
    49.  
    dont focus on everyfunction that have a mainmenu in it