首页 > 解决方案 > 脚本的整数不会使用另一个脚本更改

问题描述

所以我GameObject.Find("SAVELOADSYSTEM").GetComponent<LoadManager>().Level = Level;用来获取脚本的级别整数,脚本只是一个完成传送器。

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

public class Finish : MonoBehaviour
{

    public string DebugLog;
    public string SceneName;
    public GameObject Player;
    public int Level;
    // Start is called before the first frame update
    void Start()
    {

    }


    // Update is called once per frame
    void Update()
    {

        var distance = 3;
        if (Vector3.Distance(transform.position, Player.transform.position) < distance && Input.GetKey(KeyCode.E))
        {
            GameObject.Find("SAVELOADSYSTEM").GetComponent<LoadManager>().Level = Level;
            Debug.Log(DebugLog);
            SceneManager.LoadScene(SceneName);
        }
    }
}

那是传送器,只需靠近它并按 e(交互)。问题是我希望它更改级​​别(从此脚本)更改为(public int Level;)的级别整数值,但问题是包含要保存的级别阶段(整数)的保存加载脚本没有'不想用这个传送脚本改变值。

using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

public class LoadManager : MonoBehaviour
{
    public static LoadManager instance;
    public Data data;

    string dataFile = "737472696e67.dotouchit";

    public int Level { get; internal set; }

    void Awake()
    {
        if (instance == null)
        {
            DontDestroyOnLoad(this.gameObject);
            instance = this;
        }
        else if (instance != this)
        {
            Destroy(this.gameObject);
        }
    }

    private void Start()
    {
        Load();
    }

    private void Update()
    {
        Save();
    }
    public void Load()
    {
        string filePath = Application.persistentDataPath + "/" + dataFile;
        BinaryFormatter bf = new BinaryFormatter();
        if (File.Exists(filePath))
        {
            FileStream file = File.Open(filePath, FileMode.Open);
            Data loaded = (Data)bf.Deserialize(file);
            data = loaded;
            file.Close();


        }
        Debug.Log("Loaded Data");
    }
    public void Save()
    {
        string filePath = Application.persistentDataPath + "/" + dataFile;
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create(filePath);
        bf.Serialize(file, data);
        file.Close();

        Debug.Log("Saved Data");
    }

    [System.Serializable]
    public class Data
    {
        public int level = 0;


        public Data()
        {

            level = 0;
        }

    }


}

这就是保存加载系统脚本。我不知道为什么它不想改变水平值。

标签: c#unity3d

解决方案


可以肯定的是,该Level值已正确分配。好吧,不正确,但让我们稍后再讨论这个问题。

问题是,类中的levelData永远不会被分配。您必须使用构造函数对其进行初始化,例如:

public Data(int level)
{

    this.level = level;
}

或者,

public void Save()
{
    data.level = level;

    ...

没有分配,您的level价值将永远0有价值。也许聪明的 Intelisense 已经注意到了你,说value Level is assigned, but never used.


关于我上面提到的问题,你为什么要做这件事,

GameObject.Find("SAVELOADSYSTEM").GetComponent<LoadManager>().Level = Level;

好像你已经LoadManager用单例模式声明了,所以没有必要这样做。如果LoadManager组件正确附加到游戏对象,只需执行

LoadManager.instance.Level = Level;

就足够了。

UnityGameObject.Find()是一个超级重的功能,因为它会暴力破解场景中所有游戏对象的名称。调用一次就可以了,但是把这个东西放进去Update()好吧,在一秒钟内进行60 次蛮力并不是很好的情况。这就是为什么GameObject.Find()建议使用,仅在Start(), Awake(), 或类的初始化中。


推荐阅读