【问题标题】:NullReferenceException Can't figure out what's NullNullReferenceException 无法弄清楚什么是 Null
【发布时间】:2013-06-30 04:49:56
【问题描述】:

我在 Unity 中收到此错误:

NullReferenceException:对象引用未设置为对象的实例 TowerSlot.OnGUI () (at Assets/TowerSlot.cs:26)

我对 Unity 比较陌生,无法弄清楚这个错误来自哪一行(我假设是 26 行),我不知道什么是 null。如果有人可以帮助向我解释如何理解错误所指向的内容以及我需要做什么,将不胜感激。

TowerSlot.cs:

using UnityEngine;
using System.Collections;

public class TowerSlot : MonoBehaviour {
    public GUISkin skin = null;

    bool gui = false;

    // Tower prefab
    public Tower towerPrefab = null;

    void OnGUI() {    
        if (gui) {
            GUI.skin = skin;

            // get 3d position on screen        
            Vector3 v = Camera.main.WorldToScreenPoint(transform.position);

            // convert to gui coordinates
            v = new Vector2(v.x, Screen.height - v.y); 

            // creation menu for tower
            int width = 200;
            int height = 40;
            Rect r = new Rect(v.x - width / 2, v.y - height / 2, width, height);
            GUI.contentColor = (Player.gold >= towerPrefab.buildPrice ? Color.green : Color.red);
            GUI.Box(r, "Build " + towerPrefab.name + "(" + towerPrefab.buildPrice + " gold)");

            // mouse not down anymore and mouse over the box? then build the tower                
            if (Event.current.type == EventType.MouseUp && 
                r.Contains(Event.current.mousePosition) &&
                Player.gold >= towerPrefab.buildPrice) {
                // decrease gold
                Player.gold -= towerPrefab.buildPrice;

                // instantiate
                Instantiate(towerPrefab, transform.position, Quaternion.identity);

                // disable gameobject
                gameObject.SetActive(false);
            }
        }
    }

    public void OnMouseDown() {
        gui = true;
    }


    public void OnMouseUp() {
        gui = false;
    }
}

我也正在尝试在http://makeagame.info/unity-tower-defense-game-step-4-scripting这里学习本教程

谢谢!

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您在早期将 towerPrefab 设置为 null,然后在第 26 行,您在为其分配任何非 null 值之前引用其 buildPrice 属性或字段。这将抛出一个空异常。

    这行是问题所在:

    GUI.contentColor = (Player.gold >= towerPrefab.buildPrice ? Color.green : Color.red);
    

    【讨论】:

    • 公共塔 towerPrefab = null 正在 Unity 中创建公共属性。 OP 可能忘记在 Unity 编辑器中为其分配值 - 而不是在代码中。
    • @theodox - 我看得太简单了,并认为 towerPrefab 是方法中的本地变量。我仍然认为 towerPrefab 是空的。在执行该行之前,它没有设置为非 null。
    • 这是我在统一编辑器gyazo.com/90166d36a096ee0953d5f4f386d32c0c 中看到的内容,如果我尝试添加其他内容,资产和场景将显示为空。我是否分配了它?我不确定,因为它在无后括号中表示预制件的名称...
    【解决方案2】:

    OP 回复@Hatchet 的截图肯定显示了塔的预制没有在编辑器中设置。

    它应该由其他代码设置(不起作用),或者应该手动设置。您可以通过将塔预制拖到插槽上,或单击“无(塔)”右侧的小圆圈来执行此操作:这将弹出一个选择器对话框,您可以在其中选择塔预制。它显示为“塔”,因为这是它想要的变量。

    顺便说一句,GUIskin 也没有设置。当你打线时,这可能会导致问题

    GUI.skin = skin;
    

    Unity gui 上用于分配此类引用的文档在这里:http://docs.unity3d.com/Documentation/Manual/EditingReferenceProperties.html

    【讨论】:

      【解决方案3】:

      hatchet 指出了问题所在。我认为这可能会解决它:

      public Tower towerPrefab = new Tower();
      

      因为Tower 类具有您尝试访问的属性buildPrice

      【讨论】:

      • 我试过了,得到了同样的 null 错误以及两个警告:您正在尝试使用“new”关键字创建 MonoBehaviour。这是不允许的。 MonoBehaviours 只能使用 AddComponent() 添加。或者,您的脚本可以从 ScriptableObject 继承或根本没有基类,并且您正在尝试使用“new”关键字创建 MonoBehaviour。这是不允许的。 MonoBehaviours 只能使用 AddComponent() 添加。或者,您的脚本可以继承 ScriptableObject 或根本不继承 UnityEngine.MonoBehaviour:.ctor() Tower:.ctor() TowerSlot:.ctor()
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 2021-07-10
      相关资源
      最近更新 更多