如何将值发送到放在其他脚本中的函数?

时间:2015-01-28 05:12:37

标签: c# unity3d

我正在尝试发送变量(值?)作为参数。该函数位于UIRoot(ngui)附带的另一个脚本中。

以下是我的代码。

labelTest脚本,包含函数:

public class labelTest : MonoBehaviour {

    private string val = "hi i'm val";

    // Use this for initialization
    void Start () {
        print ("hello");
    }

    // Update is called once per frame
    void Update () {
        print ("update");
        //changeLabel ();
        //changeLabel (val);
        print ("come out changeLabel Func");
    }

    public void changeLabel () {
        print("Im going to change LabelText");
        GameObject gol = GameObject.Find ("gol");
        UILabel s = gol.GetComponent<UILabel> ();
        s.text = "hello";
        print (s.text);
    }

    public void changeLabel (string var) {
        print("Im going to change LabelText to var");
        GameObject gol = GameObject.Find ("gol");
        UILabel s = gol.GetComponent<UILabel> ();
        s.text = var;
        print (s.text);
    }
}

CheckTime脚本,我必须将值发送到LabelTest函数:

using UnityEngine;
using System.Collections;

public class CheckTime : MonoBehaviour {

    public delegate void listener( ArrayList touches );
    public event listener touchBegin, touchMove, touchEnd;

    float touchTime;

    void Start () {
        print ("checkTime Script Start()");
        touchTime = 0;
        touchBegin += ( touches ) =>{ Debug.Log( touchTime ); };
        touchEnd += ( touches )=>{ Debug.Log( touchTime ); };
        touchMove+= ( touches )=>{ Debug.Log( touchTime ); };
    }

    void Update () {
        print ("sssss");
        int count = Input.touchCount;
        if( count == 0 ) return;
        print (count);
        bool begin, move, end;
        begin = move = end = false;

        GameObject gol = GameObject.Find ("gol");
        labelTest go = gol.GetComponent<labelTest>();

        ArrayList result = new ArrayList();

        for( int i = 0 ; i<count ; i++ ){ 
            Touch touch = Input.GetTouch(i);
            result.Add( touch ); //보낼 인자에 추가
            if(touch.phase==TouchPhase.Began&&touchBegin!=null) {begin = true; touchTime += Time.deltaTime; Debug.Log(touchTime);}
            else if(touch.phase==TouchPhase.Moved&&touchMove!=null) { move = true; touchTime += Time.deltaTime; Debug.Log(touchTime);}
            else if(touch.phase==TouchPhase.Ended&&touchEnd!=null) { end = true; touchTime=0; Debug.Log(touchTime);}
            //here!
            print ("im in checkTime script");
            string s = touchTime.ToString(); //// touchTime -> float to string
            go.changeLabel(s); 
            // NullReferenceException : Object reference not set to an instance of an object CheckTime.Update()

            Debug.Log(touchTime);
        }

        //포인트중 하나라도 상태를 가졌다면..
        if( begin ) touchBegin(result);
        if( end ) touchEnd(result);
        if( move ) touchMove(result);

    }
}

我要做的是将CheckTime脚本中的touchTime(值)作为参数发送到changeLabel()函数并显示在Label Text上。

但是,我有一个NullReferenceException。我详细检查了脚本。我怎样才能解决这个问题?请让我知道我现在缺少什么。谢谢。

2 个答案:

答案 0 :(得分:2)

确保将labelTest脚本附加到GameObject gol以使脚本正常工作,这是您刚才获得空引用异常的唯一原因。

除此之外,请勿在更新功能中保留这两行:

GameObject gol = GameObject.Find ("gol");
labelTest go = gol.GetComponent<labelTest>();

GameObject.Find和GetComponent都是昂贵的功能。如果可以,你最好在这里做一个

public labelTest go;

在脚本的顶部。然后在检查器中,您可以将标签gameObject指定为此参数。 The labelTest public variable

如果你必须在运行时分配它,那么在Start而不是Update中进行。

事实上,除非你有进一步的labelTest计划,你可以一起摆脱它并使用

public UILabel timeLabel;

位于CheckTime的顶部并更改

string s = touchTime.ToString(); //// touchTime -> float to string
        go.changeLabel(s); 

timeLabel.text = touchTime.ToString();

答案 1 :(得分:1)

您也可以使用单身方法。如果项目中只有一个实例,这种方式会更有用。

在MyManager课程中

public class MyManager : MonoBehaviour
{
    public static MyManager instance;

    void Awake() {
        instance = this;
    }

    public void MyFunction() {
        //funcion code
    }

}

然后,当您想从项目的任何地方调用此函数时,只需调用

即可
MyManager.instance.MyFunction()