如何检查当前显示的ImageTarget是否等于随机选择的ImageTarget?

时间:2015-12-29 01:05:04

标签: c# unity3d augmented-reality vuforia unity5

我发布了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的新手!如果你帮助我将不胜感激

1 个答案:

答案 0 :(得分:0)

你遇到的问题是NullReferenceException。在这种情况下,你想要达到的目标几乎无关紧要。

只需检查NullReferenceException发生的代码行,找到未分配的引用(在该行中),确保正确分配该代码并解决该问题。