0

In Unity, I have some simple code that works:

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. 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;, it throws: NullReferenceException: Object reference not set to an instance of an object. 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.

3

1 Answer 1

0

The first example you provided throws the same NullReferenceException when I tested it. It sounds like you've attached a Toggle through the GUI to the instance of Test, but not to the instance of 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. 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.

Sign up to request clarification or add additional context in comments.

3 Comments

Ah shoot, my bad then. :( Good luck! I'll post if I find anything else before someone else does.
Ok I figured out the problem. In the second code I thought it was referencing a GameObject in the hierarchy, but instead it was referencing a prefab, which, as you said, did not have the toggle selected in inspector. Since it was a prefab, I had to use getComponenet to access the toggle. It took a while, but I realized that your answer was correct! Sorry!
I believe that the strange phenomena of logging true before throwing the error was that the GameObject that was referencing the script printed it, before the prefab, which is the one I did not think I was using threw the error. Interesting mystety, that was, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.