Reputation: 1
I'm using Unity Augmented Reality. Why does my application return a null value if I call a public value of an other class?
These are my classes:
using UnityEngine;
using System.Collections;
public class GUIControlScript : MonoBehaviour {
public GUISkin MenuSkin;
public Texture BoxBackground;
public DefaultTrackableEventHandler DTEH;
public string ttx;
void OnGUI(){
Screen.orientation = ScreenOrientation.Landscape;
GUI.skin = MenuSkin;
GUI.Box(new Rect(0, 0, Screen.width, 600), BoxBackground);
GUI.BeginGroup(new Rect(Screen.width / 2 - 150, Screen.height / 2 - 150, 300, 300));
try
{
GUI.Button(new Rect(0, 0, 500, 500), ttx);
}
catch(System.Exception e)
{
}
GUI.EndGroup();
}
void Update()
{
ttx = DTEH.TrackableText; //This is the problem!
}
This is the class where I'm calling the variable
using UnityEngine;
public class DefaultTrackableEventHandler : MonoBehaviour,
ITrackableEventHandler
{
#region PRIVATE_MEMBER_VARIABLES
private TrackableBehaviour mTrackableBehaviour;
public string TrackableText = "";
#endregion // PRIVATE_MEMBER_VARIABLES
#region UNTIY_MONOBEHAVIOUR_METHODS
void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
OnTrackingLost();
}
#endregion // UNTIY_MONOBEHAVIOUR_METHODS
#region PUBLIC_METHODS
// Implementation of the ITrackableEventHandler function called when the
// tracking state changes.
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED)
{
OnTrackingFound();
}
else
{
OnTrackingLost();
}
}
#endregion // PUBLIC_METHODS
#region PRIVATE_METHODS
private void OnTrackingFound()
{
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();
// Enable rendering:
foreach (Renderer component in rendererComponents) {
component.enabled = true;
}
if (mTrackableBehaviour.TrackableName.Equals("farmasi"))
{
TrackableText = "farmasi";
}
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
}
private void OnTrackingLost()
{
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();
// Disable rendering:
foreach (Renderer component in rendererComponents) {
component.enabled = false;
}
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
}
#endregion // PRIVATE_METHODS
Upvotes: 0
Views: 1561
Reputation: 66
Since you have DefaultTrackableEventHandler
extending MonoBehaviour
, it needs to be attached to a GameObject
in order for it to initialize. You can do this in one of two ways:
In the inspector viewing the GameObject
that GUIControlScript
is attached to: add the Script
manually to the GameObject
by clicking Add Component
, choosing Scripts
(or wherever your scripts are located), and clicking on DefaultTrackableEventHandler
. In the code, you can then access the DefaultTrackableEventHandler
script by calling GetComponent:
public class GUIControlScript : MonoBehaviour {
private DefaultTrackableEventHandler dteh;
void Start() {
dteh = GetComponent<DefaultTrackableEventHandler>();
}
}
In the GUIControlScript
script, call AddComponent to programmatically add the DefaultTrackableEventHandler
script to the GameObject
:
public class GUIControlScript : MonoBehaviour {
private DefaultTrackableEventHandler dteh;
void Start() {
dteh = gameObject.AddComponent<DefaultTrackableEventHandler>();
}
}
You do not always have to manually add the Component
to the GameObject
itself. Typically if you don't have any public variables that you want to change directly in the Inspector
, then you can just programmatically add the Component
.
Upvotes: 1
Reputation: 6132
Using the discussion in the comments, we managed to narrow it down to see what is null
exactly. You call public DefaultTrackableEventHandler DTEH;
, but you never initiate it. Try adding another function in your GUIControlScript
-class like this:
void Start()
{
DTEH = new DefaultTrackableEventHandler();
}
That way, you didn't only give DTEH
a type, but you initialized it as well.
Upvotes: 1