如何检查当前显示的ImageTarget是否等于随机选择的ImageTarget
本文关键字:ImageTarget 是否 选择 于随机 显示 何检查 检查 | 更新日期: 2023-09-27 18:28:28
我发布了Quiz.cs脚本,在其中我选择了一个随机游戏对象。现在在DefaultTrackableEventHandler中,我需要检查随机选择的游戏对象的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 !");
}
}
}
}
现在,当我将此脚本调用到DefaultTrackableEventHandler.cs中的OnTrackingFound()中时(当我向相机显示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的新手!!!如果您能帮助
您遇到的问题是NullReferenceException。在这种情况下,你想要实现的目标几乎是无关紧要的。
只需检查发生NullReferenceException的代码行,找到未分配的引用(在该行中),确保正确分配,问题就会得到解决。