0

Basically, I am creating an augmented-reality project with Vuforia in Unity. I am very new to C# so the code might not be so optimal, I know but it worked. I saved and quit yesterday and today when I opened it to test again and develop more, suddenly errors are thrown which say:

NullReferenceException: Object reference not set to an instance of 
an object
marsHVB_script.Start () (at Assets/marsHVB_script.cs:11)

I have two separate scripts, one being main:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;

public class main : MonoBehaviour
{
    public static GameObject helpObj;
    public static GameObject NVB;
    public static GameObject BVB;
    public static GameObject helpMsg;
    public static GameObject nText1;
    public static GameObject nText2;
    public static GameObject nText3;
    public static GameObject nText4;
    public static int stage = 1;
    // Start is called before the first frame update
    void Start()
    {
        helpObj = GameObject.Find("marsHVB");
        helpMsg = GameObject.Find("marsHelp");
        nText1 = GameObject.Find("nText1");
        nText2 = GameObject.Find("nText2");
        nText3 = GameObject.Find("nText3");
        nText4 = GameObject.Find("nText4");
        nText2.SetActive(false);
        nText3.SetActive(false);
        nText4.SetActive(false);
        helpMsg.SetActive(false);

    }

    public static void IncrStage()
    {
        stage++;
        Debug.Log("Stage Increased to " + stage);
    }
    public static void DecrStage()
    {
        stage--;
        Debug.Log("Stage Decreased to " + stage);
    }
    public static int GetStage()
    {
        Debug.Log("Stage Got " + stage);
        return stage;
    }
    // Update is called once per frame
    void Update()
    {

    }
}

In the code presented below, I create public static variables that should be accessible from other scripts. I find them and I turn them off after.

Then, there is second script which controls the virtual button:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;

public class marsHVB_script : MonoBehaviour, IVirtualButtonEventHandler
{

    void Start()
    {
        main.helpObj.GetComponent<VirtualButtonBehaviour>().RegisterEventHandler(this);
    }

    void IVirtualButtonEventHandler.OnButtonPressed(VirtualButtonBehaviour vb)
    {
        switch (main.GetStage())
        {
            case 1:
                main.nText1.SetActive(false);
                main.helpMsg.SetActive(true);
                break;
            case 2:
                main.nText2.SetActive(false);
                main.helpMsg.SetActive(true);
                break;
            case 3:
                main.nText3.SetActive(false);
                main.helpMsg.SetActive(true);
                break;
            case 4:
                main.nText4.SetActive(false);
                main.helpMsg.SetActive(true);
                break;
        }

        Debug.Log("Help Pressed");
    }

    void IVirtualButtonEventHandler.OnButtonReleased(VirtualButtonBehaviour vb)
    {

        switch (main.GetStage())
        {
            case 1:
                main.helpMsg.SetActive(false);
                main.nText1.SetActive(true);
                break;
            case 2:
                main.helpMsg.SetActive(false);
                main.nText2.SetActive(true);
                break;
            case 3:
                main.helpMsg.SetActive(false);
                main.nText3.SetActive(true);
                break;
            case 4:
                main.helpMsg.SetActive(false);
                main.nText4.SetActive(true);
                break;
        }
        Debug.Log("Help Removed");
    }

    // Update is called once per frame
    void Update()
    {

    }
}

This second script reacts on virtual button press and release. Error is thrown on line 11 which is the main.helpObj.GetComponent function and it worked yesterday, today it does not.

Please help me troubleshoot what is the reason for this as I am stuck here now.

  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – JSteward Apr 12 '19 at 15:52
  • @JSteward How is it possible that after closing and opening the project it became null? – Giorgi Qutateladze Apr 12 '19 at 15:57
  • The order in which `Start()` is called among multiple scripts is undefined. It's entirely legal for `main.Start()` to have been running first yesterday but for `marsHVB_script.Start()` to be running first today. Can you see why `helpObj` would be null if `marsHVB_script.Start()` ran before `main.Start()`? – Jeff Apr 12 '19 at 16:00
  • What happens if you change `Start()` to `Awake()` in `main`? – Jeff Apr 12 '19 at 16:02
  • @JeffE Changed and it worked! Thank you. – Giorgi Qutateladze Apr 12 '19 at 16:56

2 Answers2

1

You should never use the Start void to initialize variables in your code. Awake is generally used for that, since Awake is always called before Start.

If you change the Start() in your main class to an Awake(), you make sure, that helpObj always gets assigned before the marsHVB_script class tries to access it.

Ali Baba
  • 330
  • 1
  • 6
  • Changing Start() to Awake() worked but for unknown reason (I did not touch anything else) unity camera started lagging seriously. Like 5 frames per second or smth. Can it be associated somehow? OR is my code too heavy to run or smth? – Giorgi Qutateladze Apr 12 '19 at 16:57
  • Shouldn't be connected to this. Try running the game in fullscreen and see if it's still lagging. Sometimes when you select certain GameObjects in the editor, like UI objects, the game can get laggy. – Ali Baba Apr 12 '19 at 17:07
0

You can use Reset() function and place all the code that you have written in Start function. it will allow you to set references without playing the game.

void Reset()
{
    helpObj = GameObject.Find("marsHVB");
    helpMsg = GameObject.Find("marsHelp");
    nText1 = GameObject.Find("nText1");
    nText2 = GameObject.Find("nText2");
    nText3 = GameObject.Find("nText3");
    nText4 = GameObject.Find("nText4");
    nText2.SetActive(false);
    nText3.SetActive(false);
    nText4.SetActive(false);
    helpMsg.SetActive(false);

}

Write this, go back to the editor. Click on the gear icon on the script component and click "Reset". It will set all the references and then you can safely remove that Start function.