c# - 为什么我的函数不能访问它所在类的私有变量 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,因为我有一个调试日志打印该对象。在那里。
解决方案
由于您的列表是非公开且非序列化的字段,因此您无法从检查器分配它们以自动创建新的列表实例,因此在 ClickHandler 脚本中添加 Awake 方法,将它们实例化为新的列表对象。
void Awake() {
selectedMinions = new List<GameObject>();
selectedHostiles = new List<GameObject>();
}
或者你可以在声明时做同样的事情
List<GameObject> selectedMinions = new List<GameObject>();
List<GameObject> selectedHostiles = new List<GameObject>();
推荐阅读
- c++ - C++ 将对象存储在具有稳定地址的向量中
- verilog - 建模 JK FF 时输出错误:输出为 x
- tensorflow - 如何使用文件路径制作用于 3d 卷积的图像立方体
- android - 具有不同片段的Android ViewPager2:错误->始终显示相同的片段
- ios - 在 Swift 中使用现有的 SQLite 数据库
- css - 为什么我点击我的标签时它打不开?
- javascript - 如何检查页面是否在 Safari 浏览器中翻译?
- excel - 具有特定值/字符串的 VBA 颜色行直到最后一列
- javascript - 两个函数之间的递归调用
- swiftui - SwiftUI 中的 ForEach 循环仅在数组中显示第一个