在 Unity (C#) 中,为什么我会收到 NullReferenceException 以及如何修复它? [英] In Unity (C#), why am I getting a NullReferenceException and how do I fix it?

查看:75
本文介绍了在 Unity (C#) 中,为什么我会收到 NullReferenceException 以及如何修复它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是使用 Unity 的初学者最常问的问题之一,所以我再问一次,以解决规范 NullReferenceException 问题未完全回答的 Unity 特定原因.


我正在 Unity 中开发一款游戏,当我点击播放时,我收到一条错误消息,显示NullReferenceException:未将对象引用设置为对象的实例";我不明白为什么会这样.是什么原因造成的,我该如何解决?

解决方案

说明

通常在 C# 中,这是由引用尚未初始化的字段引起的.例如,如果您有一个字段 public List;items,然后您在没有先执行 items = new List() 的情况下调用 items.Add(foo),然后您尝试添加一个项目到一个不存在的列表.

然而,特别是在 Unity 中,这最常见的原因是忘记在检查器中设置引用.当您创建一个新组件并添加一个字段 public Transform destination 时,您很可能打算在检查器中分配一个预制件.如果您忘记了,您就是在尝试引用不存在的内容.

解决方案

如果您双击控制台窗口中的错误消息,Unity 将(除了少数例外)在引发错误的层次结构中突出显示游戏对象,然后打开您的代码编辑器并突出显示脚本行发生错误.

  1. 如果您使用任何 Find 之类的方法来获取游戏对象,请确保游戏对象处于活动状态,否则 Find 之类的方法将返回 null 或可能返回错误的 GameObject.如果您需要在找到游戏对象时使其处于非活动状态,则需要使用与使用类似 Find 的方法直接查找游戏对象不同的方法来获取对游戏对象的引用,例如使用组件向 Manager 类型的类注册.

  2. 查看游戏对象,确保您已在检查器中分配了应分配的所有内容.

  3. 如果所有内容都已分配,请使用抛出错误的游戏对象运行游戏.您可能在 Awake()Start() 中有一些否定引用的内容,您将看到检查器切换到 None.

  1. 注意用于修改对象的方法的返回类型.例如,如果您在对象上调用 GetComponent() 或任何类似的东西,但未找到该组件,则不会抛出错误.它只会返回空值.这可以通过如下一行轻松处理:

    if(thing == null)//记录一个错误,或者做一些事情来修复引用 else//做你想做的事情

这应该涵盖最常见的 Unity 特定原因.如果这仍然不能解决您的问题,请查看 Unity 自己的页面 on NullReferenceException,以及C#一般,有更深入的解释 NullReferenceException 在这个答案中.

This is one of the most frequent questions asked by beginners using Unity so I'm asking it one more time to address the Unity-specific causes not fully answered by the canonical NullReferenceException question.


I'm working on a game in Unity and when I hit play, I got an error that says "NullReferenceException: Object reference not set to an instance of an object" and I can't figure out why it's happening. What is causing it and how do I fix it?

解决方案

Explanation

In C# generally, this is caused by referencing a field that hasn't been initialized. For example, if you have a field public List<GameObject> items and you later call items.Add(foo) without first doing items = new List<GameObject>(), then you are trying to add an item to a list that doesn't exist.

However, in Unity specifically, this is most frequently caused by forgetting to set a reference in the inspector. When you create a new component and add a field public Transform destination, then you most likely are intending to assign a prefab in the inspector. If you forget, you're trying to reference something that doesn't exist.

Solutions

If you double-click on the error message in the console window, Unity will (with a few exceptions) highlight the GameObject in the hierarchy that threw the error, and open your code editor and highlight the line of the script where the error occurred.

  1. If you are using any of the Find-like methods to get the GameObject, be sure that the GameObject is active, otherwise Find-like methods will return null or may return the wrong GameObject. If you need the GameObject to be inactive when it is found, you need to use a different way of getting a reference to the GameObject than using a Find-like method to find the GameObject directly, such as having the component register with a Manager-type class.

  2. Looking at the GameObject, make sure you've assigned everything in the inspector that should be assigned.

  3. If everything has been assigned, run the game with the GameObject that threw the error selected. It's possible that you have something in Awake() or Start() that's negating the reference, and you'll see the inspector switch to None.

  1. Pay attention to the return types of methods you use to modify objects. For example, if you call GetComponent() or anything similar on an object and the component is not found, it will not throw an error. It will just return null. This can easily be handled with a line like:

    if(thing == null) //log an error, or do something to fix the reference else //do what you wanted to do

That should cover the most frequent Unity-specific causes. If that still isn't fixing your issue, Unity's own page on NullReferenceException, and for C# generally, there's a more in-depth explanation of NullReferenceException in this answer.

这篇关于在 Unity (C#) 中,为什么我会收到 NullReferenceException 以及如何修复它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆