简体   繁体   English

在 Unity 中加载场景会在该场景中引用 null

[英]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.我正在 Unity 中制作我的第一款游戏,并且我正在尝试在开始的过场动画结束时加载它的第一关。 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.我无法移动我的角色,因为脚本中的所有引用都是 null。 I have no idea what I did wrong.我不知道我做错了什么。

Unity references should be stored in the scene file. Unity 引用应存储在场景文件中。 Are you using any source control like git?您是否使用任何源代码控制,例如 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.如果其他人没有将更改推送到 .unityscene 文件或关联脚本/预制件的 .meta 文件,则可能会破坏引用。

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. Unity 的公共变量应保存在场景文件中,并应始终与场景一起加载。

Double check to see if the references are actually null.仔细检查参考是否实际上是 null。 Are you using an animation when you load into the Scene?加载到场景时是否使用 animation? 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.例如,如果您在加载场景时为角色设置动画,它可能会卡在 animation 剪辑中,您将无法移动它。

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'.最后,如果您指的是在代码中创建的对其他脚本、对象或变量的引用,那么如果您不使用“DontDestroyOnLoad”,这些可能会在场景之间中断。

Just spit-balling here, I need more information to correctly find the solution.只是在这里吐口水,我需要更多信息才能正确找到解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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