C#调用其他类变量但返回null(Unity3D)

时间:2013-04-08 07:47:21

标签: android unity3d augmented-reality

我正在使用Unity增强现实。如果我调用其他类的公共值,为什么我的应用程序返回空值?

这些是我的课程:

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!
}

这是我调用变量

的类
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

2 个答案:

答案 0 :(得分:1)

使用评论中的讨论,我们设法缩小范围,以确切地了解null。你打电话给public DefaultTrackableEventHandler DTEH;,但你永远不会发起它。尝试在GUIControlScript - 类中添加另一个函数,如下所示:

void Start()
{
    DTEH = new DefaultTrackableEventHandler();
}

这样,你不仅给了DTEH一个类型,而且你也初始化了它。

答案 1 :(得分:1)

由于DefaultTrackableEventHandler扩展MonoBehaviour,因此需要将其附加到GameObject才能进行初始化。您可以通过以下两种方式之一完成此操作:

  1. 在查看GameObject附加了GUIControlScript的检查员中:点击Script,手动将GameObject添加到Add Component,然后选择Scripts(或脚本所在的位置),然后点击DefaultTrackableEventHandler。在代码中,您可以通过调用GetComponent

    来访问DefaultTrackableEventHandler脚本
    public class GUIControlScript : MonoBehaviour {
        private DefaultTrackableEventHandler dteh;
    
        void Start() {
            dteh = GetComponent<DefaultTrackableEventHandler>();
        }
    }
    
  2. GUIControlScript脚本中,致电AddComponent以编程方式将DefaultTrackableEventHandler脚本添加到GameObject

    public class GUIControlScript : MonoBehaviour {
        private DefaultTrackableEventHandler dteh;
    
        void Start() {
            dteh = gameObject.AddComponent<DefaultTrackableEventHandler>();
        }
    }
    
  3. 您无需始终手动将Component添加到GameObject本身。通常,如果您没有要在Inspector中直接更改的任何公共变量,则可以通过编程方式添加Component