-1
我发布Quiz.cs脚本,我选择随机游戏对象。现在在DefaultTrackableEventHandler中,我需要检查随机选择的游戏对象的ImageTarget是否等于相机显示的ImageTarget!如何检查当前显示的ImageTarget是否等于随机选择的ImageTarget?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Vuforia;
public class Quiz : MonoBehaviour
{
public GameObject[] models;
public GameObject currentPoint;
int index;
public AudioSource correctAudio;
public AudioSource notcorrectAudio;
//QuizLogic test ;
//public ImageTargetBehaviour checck;
//private TrackableBehaviour mTrackable;
//public GameObject[] sounds;
void Start()
{
StateManager sm = TrackerManager.Instance.GetStateManager();
IEnumerable<TrackableBehaviour> activeTrackables = sm.GetActiveTrackableBehaviours();
models = GameObject.FindGameObjectsWithTag("numbers");
index = Random.Range (0, models.Length);
currentPoint = models[index];
print ("Trackable " +currentPoint.name);
currentPoint.GetComponent<AudioSource>().Play();
foreach (TrackableBehaviour tb in activeTrackables)
{
// As you iterate, you compare with current game object name
// if you have 3 and 5 and target object is 5 then you get a match.
if(tb.GetComponent<ImageTargetBehaviour>().ImageTarget.Name==currentPoint.GetComponent<ImageTargetBehaviour>().ImageTarget.Name)
{
print ("Congratulations you have chosen the correct ImageTarget");
}
else
{
print ("Try another one !");
}
}
}
}
现在,当我把这个脚本放到OnTrackingFound()在DefaultTrackableEventHandler.cs(当我展示给相机一个ImageTarget它与任意选择ImageTarget比较)我得到这个错误: 的NullReferenceException:对象引用未设置为一个对象
这个实例是DefaultTrackableEventHandler.cs:
using UnityEngine;
namespace Vuforia
{
/// <summary>
/// A custom handler that implements the ITrackableEventHandler interface.
/// </summary>
public class QuizLogic : MonoBehaviour,
ITrackableEventHandler
{
#region PRIVATE_MEMBER_VARIABLES
private TrackableBehaviour mTrackableBehaviour;
#endregion // PRIVATE_MEMBER_VARIABLES
Quiz quiz;
#region UNTIY_MONOBEHAVIOUR_METHODS
void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
}
#endregion // UNTIY_MONOBEHAVIOUR_METHODS
#region PUBLIC_METHODS
public GameObject show;
public GameObject hide;
/// <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()
{
show.SetActive(true);
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
AudioSource[] audiocomponents = GetComponentsInChildren<AudioSource>(true);
// Enable rendering:
foreach (Renderer component in rendererComponents)
{
component.enabled = true;
}
// Enable colliders:
foreach (Collider component in colliderComponents)
{
component.enabled = true;
}
//Enable AudioSource
foreach (AudioSource component in audiocomponents)
{
component.enabled = true;
}
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
quiz.GetComponent<Quiz>();
}
private void OnTrackingLost()
{
hide.SetActive(true);
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
AudioSource[] audiocomponents = GetComponentsInChildren<AudioSource>(true);
// Disable rendering:
foreach (Renderer component in rendererComponents)
{
component.enabled = false;
}
// Disable colliders:
foreach (Collider component in colliderComponents)
{
component.enabled = false;
}
//Disable AudioSource
//foreach (AudioSource component in audiocomponents)
//{
// component.enabled = false;
//}
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
}
#endregion // PRIVATE_METHODS
}
}
请帮我,因为我是新来的Vuforia!我将不胜感激,如果你的帮助
这是给出错误的行: quiz.GetComponent(); 在这一行我打算在Default trackableEventHandler,OnTrackingFound()函数中调用测验脚本 –
Butrint
这行有两个问题。首先:'quiz'永远不会被分配一个值,因此它是空的,这意味着当你尝试调用它的一个成员时(这是发生在你身上的事情)你会得到一个NullReferenceException。第二行:'quiz.GetComponent();'没有效果。 'GetComponent'只返回组件(如果存在),它不会对它做任何事情。 –
我想要做的是检查OnTrackingFound()函数,如果从Quiz.cs中提取的随机选择的对象与向摄像机显示的对象相同。你能帮我做这个吗?我会很感激 – Butrint