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.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Demandooda
  • 122
  • 1
  • 14
  • This is a duplicated question, @Systemagical answered it correctly. – matiaslauriti Jun 21 '16 at 07:29
  • 1
    Possible duplicate of [Unity3D: NullReferenceException: Object reference not set to an instance of an object](http://stackoverflow.com/questions/17588799/unity3d-nullreferenceexception-object-reference-not-set-to-an-instance-of-an-o) – matiaslauriti Jun 21 '16 at 07:29
  • Doesn't the logging come after the line where the exception is thrown? How does it both log "true" and throw the exception on the previous line? – cjmarsh Jun 21 '16 at 10:03

1 Answers1

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.

  • Ah shoot, my bad then. :( Good luck! I'll post if I find anything else before someone else does. – Systemagical Jun 21 '16 at 02:31
  • 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! – Demandooda Jun 21 '16 at 15:32
  • 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. – Demandooda Jun 21 '16 at 15:54