首页 > 解决方案 > 为什么我的函数不能访问它所在类的私有变量 INSIDE?

问题描述

编辑:我对空错误表示什么并不感到困惑,我知道我的列表是空的。我只是不确定为什么在这种情况下它会为空,或者为什么将我的列表设置为 public 突然解决了这个问题。

因此,我正在尝试创建一个函数,如果它满足条件,它将将对象添加到列表中。该函数接受它正在检查的标签,以及如果可能的话它将添加到的特定列表。我这样做的原因是因为我有两个列表,我宁愿只需要一个函数来处理这两个列表。

这一切都很好,但我遇到了一个问题,它不......真的似乎工作,除非列表设置为公开?如果我能帮上忙,我宁愿不这样做。我不明白为什么,因为它可以很好地处理私有 gameObject 引用,并且在同一个位置声明,并且该函数在同一个类中,但是在列表为私有时尝试调用此函数只会给我一个 null对象引用错误,在这种情况下,我已将其范围缩小到为 null 的列表,而不是对象?

这没什么大不了的,因为我可以改变一些事情以避免公开列表,但我真的很困惑为什么会这样?我试过明确地通过 ref 传递,但这给出了同样的问题。

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

public class ClickHandler : MonoBehaviour
{
    GameObject clickedMob = null;

    List<GameObject> selectedMinions;
    List<GameObject> selectedHostiles;

    void Update ( )
    {

        if ( Input.GetButtonDown("Click"))
        {
            switch (clickState)
            {
                case NextClickAction.Nothing:
                    { }
                    break;

                case NextClickAction.TargetFriendly:
                    {

                       hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

                        if ( hit )
                        {
                            if ( hit.transform != null )
                            {
                                clickedMob = hit.transform.gameObject;
                                CheckObject("Minion", selectedMinions);
                            }
                        }
                    }
                    break;

                case NextClickAction.TargetHostile:
                    {
                        hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

                        if ( hit )
                        {
                            if ( hit.transform != null )
                            {
                                clickedMob = hit.transform.gameObject;
                                CheckObject("Hostile", selectedHostiles);
                            }
                        }
                    }
                    break;

                default:
                    {
                        Debug.Log("why do you have an enum this big");
                        DeactivateClick();
                        Debug.LogError("Enum set outside of index. Returned to 0.");
                        break;
                    }
            }

        }
    }   //  end of update

    private void CheckObject ( string tag , List<GameObject> mobList)
    {
        if ( clickedMob.CompareTag(tag) )
        {
            if ( clickedMob.GetComponent<Animator>() != null )
            {
                ClickedMobAnimator = clickedMob.GetComponent<Animator>();

                switch (ClickedMobAnimator.GetBool("Selected"))
                {
                    case true:
                        ClickedMobAnimator.SetBool("Selected", false); // Setting the object to have a little box around it when it's selected.

                        mobList.Remove(clickedMob);
                        Debug.Log(mobList.Count + " " + clickedMob);
                        break;

                    case false:
                        ClickedMobAnimator.SetBool("Selected", true);
                        mobList.Add(clickedMob); 
                        Debug.Log(mobList.Count + " " + clickedMob);
                        break;
                }
            }

        }
    }

此代码的输出:

NullReferenceException: Object reference not set to an instance of an object
ClickHandler.CheckObject (System.String tag, System.Collections.Generic.List'[T] mobList) (at Assets/Scripts/ClickHandler.cs:119)

而且我知道在这种情况下该对象不是 null,因为我有一个调试日志打印该对象。在那里。

标签: c#unity3dprivate

解决方案


由于您的列表是非公开且非序列化的字段,因此您无法从检查器分配它们以自动创建新的列表实例,因此在 ClickHandler 脚本中添加 Awake 方法,将它们实例化为新的列表对象。

void Awake() {
    selectedMinions = new List<GameObject>();
    selectedHostiles = new List<GameObject>();
}

或者你可以在声明时做同样的事情

List<GameObject> selectedMinions = new List<GameObject>();
List<GameObject> selectedHostiles = new List<GameObject>();

推荐阅读