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.
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.