【问题标题】:Reference to a variable in another class yields NULL [duplicate]引用另一个类中的变量会产生 NULL [重复]
【发布时间】:2017-03-18 23:21:06
【问题描述】:

我有两个类,genDicTable 和 StartGame。我想在 StartGame 中从 genDicTable 中引用一个变量,但它产生 NULL。

genDicTable.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class genDicTable : MonoBehaviour
{
    public TextAsset file;
    public double masterCount;

    private void Start()
    {
        Load(file);

        masterCount = rowList.Count;
        Debug.Log(masterCount); // <-- This properly prints out the value of masterCount    
    }

    public class Row
    {
        public string id;
        public string word;
        public string length;
    }

    public List<Row> rowList = new List<Row>();

    public void Load(TextAsset csv) {
        // This function assigns a value into RowList
    }
}

StartGame.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StartGame : MonoBehaviour {

    public genDicTable GEN;    

    private void Start()
    {
        Debug.Log(GEN.masterCount); // <-- This yields NULL.
    }
}

所以,问题是当我访问 StartGame.cs 中的变量 masterCount 时,它会产生错误“NullReferenceException: Object reference not set to an instance of an object。”

我在这里错过了什么?

【问题讨论】:

  • 正如它所说的值为空。这种行为没有错。您尚未在 startgame.cs 中为 genDictTable 类型的变量 GEN 创建任何实例
  • 请仔细阅读指出的副本。如果你不知道什么是实例以及如何初始化它,那么这里给出的答案都不能帮助你理解你的问题并避免将来因为同样的原因而出现失误

标签: c# unity3d


【解决方案1】:

您需要对该对象的引用。

如果您的班级与您的StartGame 附加到同一个对象,那么您可以这样做:public genDicTable GEN = GetComponent&lt;genDicTable&gt;();

如果您的类附加到不同的对象,则 public genDicTable GEN = GameObject.Find("YourOtherObjectName").GetComponent&lt;genDicTable&gt;();

编辑

如果genDicTable 脚本存在并且您只想引用它:

public class StartGame : MonoBehaviour {

    public genDicTable GEN;    

    private void Start()
    {
        GEN = GameObject.Find("your object's name").GetComponent<genDicTable>();`
        Debug.Log(GEN.masterCount); // <-- This yields NULL.
    }
}

如果genDicTable 脚​​本确实 存在并且您只想创建它的新实例,请检查this 答案。

【讨论】:

  • 谢谢,但您的建议会导致“UnityException:不允许从 MonoBehaviour 构造函数(或实例字段初始化程序)调用 Find,而是在 Awake 或 Start 中调用它”错误。我也确实在 Start 中调用了它,我的变量的值变成了 0,这不是正确的值。
  • @Dongsan 修改了他的答案。再次检查。您必须在函数内执行GetComponent
  • 感谢 FCin 和所有其他顾问。最后我弄清楚出了什么问题,问题是两个脚本执行的顺序。原来,referrer 是在被引用之前被执行的。这就是为什么我没有得到(或得到错误)我想要的值。我把genDicTable中的Start()改成Awake(),问题就解决了。各位,请原谅我的愚蠢。
【解决方案2】:
public static double masterCount;

不太记得统一编辑器是如何工作的,但看起来好像您还没有创建 genDicTable 的实例,因此双精度必须是静态的才能访问。

【讨论】:

  • 我已经尝试添加“静态”,但没有运气...
  • genDicTable 的启动方法是否曾经运行过?
  • 是的,我检查了 genDicTable 中的所有内容都可以正常工作并产生它应该产生的结果。问题是如何从另一个类中引用...
  • 把它作为静态然后 genDicTable.masterCount 而不是 GEN.masterCount
  • 没有。如果genDicTable 有多个实例,就会出现问题。 GetComponent 是 OP 正在寻找的。​​span>
【解决方案3】:

您永远不会初始化该字段。改成这个

public genDicTable GEN = new genDicTable(); 

或在构造函数中执行:

public StartGame()
{
    GEN = new genDicTable();
}

【讨论】:

  • 谢谢,但是 new genDicTable() 会产生另一个警告,说“您正在尝试使用 'new' 关键字创建 MonoBehavior。这是不允许的......”
  • 最好是为这个问题再开一张票。不幸的是,我从未使用过MonoBehaviour,因此无法为您提供帮助。
  • can't 使用 new 关键字创建继承自 MonoBehaviour 的新脚本实例。
猜你喜欢
  • 1970-01-01
  • 2013-08-05
  • 2012-11-21
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多