在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的原因.

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.

我正在Unity中开发游戏,当我打游戏时,出现错误消息"NullReferenceException:对象引用未设置为对象实例".我不知道为什么会这样.是什么原因造成的,我该如何解决?

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?

推荐答案

说明

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

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.

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

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.

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

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

  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.

查看GameObject,确保已在检查器中分配了应分配的所有内容.

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

如果已分配所有内容,请使用选择了错误的GameObject运行游戏.您可能在 Awake() Start()中有一些东西使引用无效,并且您会看到检查器切换到 None

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. 请注意用于修改对象的方法的返回类型.例如,如果在对象上调用 GetComponent()或类似的东西,但找不到该组件,则不会抛出错误.它将仅返回null.可以使用以下行轻松处理:

  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)//记录错误,或执行某些操作以修复引用,否则//执行您想做的事情

这应该涵盖最常见的Unity特定原因.如果那仍然不能解决您的问题,请在NullReferenceException上使用Unity自己的页面 通常使用C#,对NullReferenceException

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天全站免登陆