【问题标题】:Strange Null Reference Error while adding element to IList向 IList 添加元素时出现奇怪的空引用错误
【发布时间】:2016-04-10 22:36:14
【问题描述】:

我遇到了一个非常令人困惑的问题,即空引用错误。内容如下:

NullReferenceException: Object reference not set to an instance of an object
GameHandler.Update () (at Assets/Scripts/GameHandler.cs:33)

这是冗长的上下文:

C#

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

public class GameHandler : MonoBehaviour {
public GameObject canvas;

IList<GameObject> selected;
GameObject[] troopObjects;
bool isSelecting = false;
Vector3 mousePosition1;

void Update() {
    // If we press the left mouse button, save mouse location and begin selection
    if (Input.GetMouseButtonDown(0)) {
        isSelecting = true;
        mousePosition1 = Input.mousePosition;
        if(selected != null) {
            foreach (GameObject selectedTroop in selected) {
                selectedTroop.transform.GetChild(0).gameObject.SetActive(false);
            };
        };
        selected = null;
    };
    // If we let go of the left mouse button, end selection
    if (Input.GetMouseButtonUp(0)) {
        isSelecting = false;
    };
    if (isSelecting) {
        troopObjects = GameObject.FindGameObjectsWithTag("Troop");
        foreach (GameObject troop in troopObjects) {
            if (IsWithinSelectionBounds(troop)) {
                selected.Add(troop);
                troop.transform.GetChild(0).gameObject.SetActive(true);
            };
        };
    };
    if (selected != null) {

    };

}
// Use this for initialization
void Start() {
    canvas.SetActive(false);
}

void OnGUI() {
    if (isSelecting) {
        // Create a rect from both mouse positions
        var rect = Utils.GetScreenRect(mousePosition1, Input.mousePosition);
        Utils.DrawScreenRect(rect, new Color(0.8f, 0.8f, 0.95f, 0.25f));
        Utils.DrawScreenRectBorder(rect, 2, new Color(0.8f, 0.8f, 0.95f));
    }
}

public bool IsWithinSelectionBounds(GameObject gameObject) {
    if (!isSelecting)
        return false;

    var camera = Camera.main;
    var viewportBounds = Utils.GetViewportBounds(camera, mousePosition1, Input.mousePosition);
    return viewportBounds.Contains(camera.WorldToViewportPoint(gameObject.transform.position));
}
}

现在问题代码似乎就在这里:

if (isSelecting) {
    troopObjects = GameObject.FindGameObjectsWithTag("Troop");
    foreach (GameObject troop in troopObjects) {
        if (IsWithinSelectionBounds(troop)) {
            selected.Add(troop);
            troop.transform.GetChild(0).gameObject.SetActive(true);
        };
    };
};

错误所指的是:

selected.Add(troop);

它是说“部队”是一个空引用。现在,当我删除这行代码时,其余的工作正常。这是没有意义的,因为在那个问题代码之后,有这个:

troop.transform.GetChild(0).gameObject.SetActive(true);

它使用相同的“部队”参考。我很想在这个问题上得到一些帮助,因为它让我很难过。如果需要任何额外信息,请告诉我。

【问题讨论】:

  • 嗯,我很确定 selectednull,而不是 troop。这不像是禁止将 null 值添加到列表中......事实上,您永远不会填写 selected 并且您将其明确设置为 null :)
  • 部队不为空,列表为!

标签: c# list unity3d nullreferenceexception


【解决方案1】:

你确定部队是空的,没有被选中吗?您上面的代码检查已选中,如果它不为空,则将其设为空。

selected = null;

【讨论】:

    【解决方案2】:

    您正在使实例无效,然后根据某些条件尝试将元素添加到 NULL 对象。这是不对的。

    请更改此行:

    selected = null;
    

    到这里:

    selected.Clear();
    

    并在开头实例化列表:

    void Update() {
    selected = new List<GameObject>();
    

    【讨论】:

    • 为什么这里是反对票?我想提高我的理解。
    猜你喜欢
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多