- Home /
Can't use any variable from different class/ NullReferenceExcept
Hi everyone! I'm using Vuforia engine with Unity to develop AR apps. I am newbie so got following problems. When I'm trying to get variable from different class in my sources folder. Everything goes well until the target variable is triggered to be change. I have a custom hud which is GUITexture. I want to make it enabled or disabled depending on another class' (Which is ImageTargetTrackabelEventHandler) isBeingTracked boolean variable. During runtime i get error like this:
NullReferenceException: Object reference not set to an instance of an object HudBehaviour.Update () (at Assets/Scripts/HudBehaviour.cs)
My code is following: HudBehaviour Class:
Public class HudBehaviour : MonoBehaviour {
public GameObject transfer;
private GUITexture hud;
private ImageTargetTrackableEventHandler checker;
void Awake(){
hud = GetComponent<GUITexture> ();
checker = transfer.GetComponent<ImageTargetTrackableEventHandler> ();
}
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update () {
if (checker.isBeingTracked==true)
{
hud.guiTexture.enabled=true;
}
else if (Input.GetKeyDown(KeyCode.A)) {
hud.enabled=true;
}
//else {
// hud.enabled=false;
// }
}
}
Second, ImageTargetTrackableEventHandler Class is like following:
public class ImageTargetTrackableEventHandler : MonoBehaviour,
ITrackableEventHandler
{
#region PUBLIC_MEMBER_VARIABLES
public bool isBeingTracked=false;
#endregion PUBLIC_MEMBER_VARIABLES
#region PRIVATE_MEMBER_VARIABLES
private TrackableBehaviour mTrackableBehaviour;
#endregion // PRIVATE_MEMBER_VARIABLES
#region PUBLIC_METODS
void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
}
/// <summary>
/// Implementation of the ITrackableEventHandler function called when the
/// tracking state changes.
/// </summary>
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
OnTrackingFound();
}
else
{
OnTrackingLost();
}
}
#endregion // PUBLIC_METHODS
#region PRIVATE_METHODS
private void OnTrackingFound()
{
isBeingTracked = true;
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
// Enable rendering:
foreach (Renderer component in rendererComponents)
{
component.enabled = true;
}
// Enable colliders:
foreach (Collider component in colliderComponents)
{
component.enabled = true;
}
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
}
private void OnTrackingLost()
{
isBeingTracked = false;
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
// Disable rendering:
foreach (Renderer component in rendererComponents)
{
component.enabled = false;
}
// Disable colliders:
foreach (Collider component in colliderComponents)
{
component.enabled = false;
}
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
}
#endregion // PRIVATE_METHODS
}
Please respond anyone who familiar with such problem. Thanks!
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Screen.width ~ HealthBar problem - C# 2 Answers
Multiple Cars not working 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
What is setter and getters??,What is setter and getters 3 Answers