Unity C#NullReferenceException [英] Unity C# NullReferenceException

查看:130
本文介绍了Unity C#NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,这个问题在这里已经得到了很多回答,相信我,我已经尝试了很多方法来解决该问题,但是它又一次又一次地发生.

I know, this question was already answered a lot here, and believe me, I've tried so many ways to fix that problem, but it occurs over and over again.

因此,基本上,我正在尝试更改从我的游戏中赚钱,来自其他脚本.

So basically, I'm trying to change e.g. money in my game, from a different script.

但是,一旦我单击按钮,我就会收到此错误消息.我认为我在这里做任何根本上错误的事情,但这也发生在我的分数脚本中,但是无论如何仍在起作用...但这是错误:

But as soon as I click the button, I get this error message. I think I'm doing anything fundamentally wrong here, but it also happens in my score script, but that is still working anyhow... But here is the error:

NullReferenceException: Object reference not set to an instance of an object
Score.ResetScore () (at Assets/Scripts/Score.cs:36)

这是一起使用的脚本.

脚本1:

void ResetScore()
    {
        GameManager gamemanag = GetComponent<GameManager>();
        score = 0;
        gamemanag.ResetQuestions();
    }

脚本2:

public void ResetQuestions()
    {
        unansweredQuestions = questions.ToList<Question>();
    }

那是得分脚本,因为它更干净.这实际上并没有奏效,我也不知道为什么...

That was the score script because it's a bit cleaner. This doesn't really work as it should as well and I have no Idea why...

我在结尾处将完整代码发布到pastebin上.

I'm posting the full code on pastebin at the end.

如果您能提供帮助,那就太好了!

Would be great if you could help!

脚本1: http://pastebin.com/raw/qvbFYd3x

脚本2: http://pastebin.com/raw/8gMzaagq

推荐答案

发生了很多事情:

  1. (如果分数== 0,为什么将分数设置为0?您可以离开 其他).

  1. (Why are you setting score to 0 if score == 0? You could just leave the else away.)

DisplayScore中,您正在访问scoreText 场地.如果您还没有的话,将抛出一个 NullReferenceException 在检查器中设置它.

In DisplayScore you are accessing the scoreText field. That will throw a NullReferenceException if you haven't set it in the inspector.

ResetScore中,您正在 GameManager实例.对ResetQuestions的调用将引发一个 NullReferenceException ,如果您包含Score脚本的游戏对象也不包含GameManager脚本.

In ResetScore you are getting the GameManager instance. The call to ResetQuestions will throw a NullReferenceException if your gameobject that contains the Score script doesn't contain the GameManager script too.

GameManagerStart中,如果您未在检查器中设置任何问题,则可能会收到 NullReferenceException .

In Start of the GameManager you could get a NullReferenceException if you haven't set any question in the inspector.

您的SetCurrentQuestion出现错误一个.基本上,如果没有问题,您将获得一个从0到0的随机数.在这种情况下,唯一有效的结果是0.然后,您正在访问索引为0的问题.但是不会有一个. (这将引发 IndexOutOfRangeException )

You have an off by one error in your SetCurrentQuestion. Basically if there are no questions you are getting a random number from 0 to 0. The only valid result is 0 in that situation. You are then accessing question with index 0. But there won't be one. (That would throw an IndexOutOfRangeException)

如果未在检查器中分配任何私有[SerializeField]字段,则都可能引发NullReferenceException.

Any of your private [SerializeField] fields could throw a NullReferenceException if it hasn't been assigned in the inspector.

如您所见,您的许多语句可能会导致 NullReferenceException .我们实际上无法告诉您问题出在哪里,因为问题可能存在很多地方.最好通过调试代码并检查尝试访问的变量为null的位置来获得最佳状态.

As you can see, many of your statements could result in a NullReferenceException. We won't be able to actually tell you where the issue is, because it could be in so many places. You are best off by debugging your code and checking where a variable you are trying to access is null.

这篇关于Unity C#NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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