简体   繁体   English

Unity 切换抛出 NullReferenceException:未将对象引用设置为对象的实例

[英]Unity toggle throws NullReferenceException: Object reference not set to an instance of an object

In Unity, I have some simple code that works:在 Unity 中,我有一些简单的代码可以工作:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Test : MonoBehaviour {

    public Toggle GridToggle;

    // Use this for initialization
    void Start () {
        GridToggle.isOn = true;
        print (GridToggle.isOn);
    }

}

As I said, this works just fine, logging 'true' to the console.正如我所说,这工作得很好,将“true”记录到控制台。 However, I have a second batch of code here, that is almost the exact same, but for some bizarre reason it does not seem to work:但是,我这里有第二批代码,几乎完全相同,但由于某些奇怪的原因,它似乎不起作用:

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

public class GridManager : MonoBehaviour {

    public Sprite[] gridColors = new Sprite[0];
    public int defaultSprite = 0;

    public Toggle GridToggle;

    public SpriteRenderer spriteRenderer;

    // Use this for initialization
    void Start () {

        //Same exact thing!
        GridToggle.isOn = true;
        print (GridToggle.isOn);
        //Same exact thing!

        spriteRenderer = GetComponent<SpriteRenderer> ();
        spriteRenderer.sprite = gridColors [defaultSprite];
    }

    void Update () {
        spriteRenderer = GetComponent<SpriteRenderer> ();
        spriteRenderer.enabled = true;
     }
}

What this does is strange: It logs 'true' to the console, but at the same time, on the line that says GridToggle.isOn = true;这样做很奇怪:它将“true”记录到控制台,但同时,在显示GridToggle.isOn = true;的行上GridToggle.isOn = true; , it throws: NullReferenceException: Object reference not set to an instance of an object. ,它抛出: NullReferenceException:未将对象引用设置为对象的实例。 I want the second code to work, but I cannot figure out what I am doing wrong, and how it is any different from the first bit.我希望第二个代码可以工作,但我无法弄清楚我做错了什么,以及它与第一个代码有何不同。

The first example you provided throws the same NullReferenceException when I tested it.您提供的第一个示例在我测试时抛出了相同的 NullReferenceException。 It sounds like you've attached a Toggle through the GUI to the instance of Test, but not to the instance of GridManager.听起来您已通过 GUI 将 Toggle 附加到 Test 实例,但未附加到 GridManager 实例。

Check in the scene GUI where you have GridManager attached to a GameObject, and make sure that it doesn't say Grid Toggle : None (Toggle) in the script details.检查将 GridManager 附加到游戏对象的场景 GUI,并确保它没有在脚本详细信息中显示 Grid Toggle : None (Toggle)。 If it does, that's your problem right there.如果是这样,那就是你的问题。 Do whatever you did to assign the Toggle object to the Test code to assign it to the GridManager code.执行任何操作将 Toggle 对象分配给 Test 代码以将其分配给 GridManager 代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ManualResetEvent引发NullReferenceException:对象引用未设置为对象的实例 - ManualResetEvent throws NullReferenceException: Object reference not set to an instance of an object ManualResetEvent.WaitOne()抛出NullReferenceException:对象引用未设置为对象的实例 - ManualResetEvent.WaitOne() throws NullReferenceException: Object reference not set to an instance of an object unity c# - NullReferenceException:未将对象引用设置为对象的实例 - Unity c# - NullReferenceException: Object reference not set to an instance of an object Unity 中的 Json 序列化 - NullReferenceException:未将对象引用设置为对象的实例 - Json Serialization in Unity - NullReferenceException : Object reference not set to an instance of an object NullReferenceException:Object 引用未设置为 object Unity2D 的实例 - NullReferenceException: Object reference not set to an instance of an object Unity2D Unity5 NullReferenceException:对象引用未设置为对象CoapManager的实例 - Unity5 NullReferenceException: Object reference not set to an instance of an object CoapManager NullReferenceException:对象引用未设置为对象#2的实例 - NullReferenceException: Object reference not set to an instance of an object #2 错误:NullReferenceException:未将对象引用设置为对象的实例 - ERROR: NullReferenceException: Object reference not set to an instance of an object NullreferenceException - 对象引用未设置为对象的实例 - NullreferenceException - Object Reference is not set to an instance of a object 未处理NullReferenceException,未将对象引用设置为对象的实例 - NullReferenceException was unhandled, Object Reference not set to an instance of an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM