Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d 不将新值传递给尚未销毁的对象_Unity3d - Fatal编程技术网

Unity3d 不将新值传递给尚未销毁的对象

Unity3d 不将新值传递给尚未销毁的对象,unity3d,Unity3d,我有两个场景,希望将信息从一个传递到另一个。在第一个场景中,我在一个空的游戏对象上创建了一个脚本常量,它保留了我希望为下一个场景保存的数据 这是: public class Constants : MonoBehaviour { public string scratchImageFront; public string scratchImageBack; void setScratchFront(string _scratchFront) {

我有两个场景,希望将信息从一个传递到另一个。在第一个场景中,我在一个空的游戏对象上创建了一个脚本常量,它保留了我希望为下一个场景保存的数据

这是:

public class Constants : MonoBehaviour {

    public string scratchImageFront;
    public string scratchImageBack;

    void setScratchFront(string _scratchFront)
    {
       scratchImageFront = _scratchFront;
    }

    void setScratchBack(string _scratchBack)
    {   
       scratchImageBack = _scratchBack;
    }

} 
在移动到下一场景之前,我调用不要销毁常量脚本:

     GameObject constants = GameObject.Find("Constants");
     Constants script;
     script = constants.transform.GetComponentInChildren<Constants>();

     DontDestroyOnLoad(constants);
     Application.LoadLevel("scratch");
重复我第一次做的步骤,常量脚本不保留传递给它的新字符串,它保留旧字符串。为什么?


我需要在我的第二个场景中销毁常量脚本,以便一切正常工作,但我不希望这样。我做错了什么?

你应该有一个“引导”场景,里面有所有不会被破坏的东西。这将是第一幕。它将调用您的“开始”(没有
常量),您将始终从后续场景加载“开始”,也就是说,不会创建新的
常量,因为“引导”在开始时总是调用一次;这里有一个关于答案的同一个问题的链接。unity:

我猜您的末尾有两个常量实例,因为
LoadLevel(“start”)
;将再次创建一个实例。我有一个脚本,我称之为“mule”,基本上就是这样做的。
Application.LoadLevel("start");