简体   繁体   中英

Loading scene in Unity makes the references in that scene null

I am making my first game in Unity and I'm trying to load the first level of it when the cutscene at the start ends. I don't know if it's possible to make the script do something after a video clip ends, so I wrote my code like this:

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

    public class CutsceneEnd : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {
            StartCoroutine("wait");
        }
        IEnumerator wait()
        {
            yield return new WaitForSeconds(36);

            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
        }

    }

But the problem is not with my method of waiting for the end of the video, it's with the scene it loads. I can't move my character because all the references in the scripts are null. I have no idea what I did wrong.

Unity references should be stored in the scene file. Are you using any source control like git? If someone else did not push the changes to the.unityscene file or the.meta files for the associated scripts/prefabs, it might break the references.

I think we need some more information here.

If you referring to references (public variables) to Assets/GameObjects in the loaded scene, then you may have simply never placed the references in the first place. Unity's public variables should be saved within the Scene file and should always load with the Scene.

Double check to see if the references are actually null. Are you using an animation when you load into the Scene? This can keep you from being able to manual move anything. For example, if you animate the character when loading the scene, it could get stuck in the animation clip and you won't be able to move it.

Lastly, if you are referring to references created within the code to other scripts, objects, or variables, then these may break between scenes if you don't utilize 'DontDestroyOnLoad'.

Just spit-balling here, I need more information to correctly find the solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM