Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将值发送到放置在其他脚本中的函数?_C#_Unity3d - Fatal编程技术网

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

C# 如何将值发送到放置在其他脚本中的函数?,c#,unity3d,C#,Unity3d,我试图发送一个变量(值?)作为参数。该函数位于附加在UIRoot(ngui)上的另一个脚本中 下面是我的代码 labelTest脚本,包括以下功能: public class labelTest : MonoBehaviour { private string val = "hi i'm val"; // Use this for initialization void Start () { print ("hello"); } //

我试图发送一个变量(值?)作为参数。该函数位于附加在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);
    }
}
GameObject gol = GameObject.Find ("gol");
labelTest go = gol.GetComponent<labelTest>();
公共类标签测试:单行为{
私有字符串val=“你好,我是val”;
//用于初始化
无效开始(){
打印(“你好”);
}
//每帧调用一次更新
无效更新(){
打印(“更新”);
//changelab();
//变更标签(val);
打印(“输出更改标签功能”);
}
公共无效更改标签(){
打印(“我要更改LabelText”);
GameObject gol=GameObject.Find(“gol”);
UILabel s=gol.GetComponent();
s、 text=“你好”;
印刷品(s.text);
}
公共void changelab(字符串变量){
打印(“我要将LabelText更改为var”);
GameObject gol=GameObject.Find(“gol”);
UILabel s=gol.GetComponent();
s、 text=var;
印刷品(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);

    }
}
使用UnityEngine;
使用系统集合;
公共类检查时间:MonoBehavior{
公共委托无效侦听器(ArrayList);
公共事件侦听器touchBegin、touchMove、touchEnd;
浮动接触时间;
无效开始(){
打印(“checkTime脚本开始()”;
接触时间=0;
touchBegin+=(touchs)=>{Debug.Log(touchTime);};
touchEnd+=(touchs)=>{Debug.Log(touchTime);};
touchMove+=(touchs)=>{Debug.Log(touchTime);};
}
无效更新(){
印刷品(“SSS”);
int count=Input.touchCount;
如果(计数=0)返回;
打印(计数);
开始,移动,结束;
开始=移动=结束=错误;
GameObject gol=GameObject.Find(“gol”);
labelTest go=gol.GetComponent();
ArrayList结果=新建ArrayList();
for(int i=0;i浮动到字符串
go.更换标签;
//NullReferenceException:对象引用未设置为对象CheckTime.Update()的实例
Debug.Log(touchTime);
}
//포인트중 하나라도 상태를 가졌다면..
如果(开始)触摸开始(结果);
如果(结束)触摸结束(结果);
如果(移动)触摸移动(结果);
}
}
我要做的是将CheckTime脚本中的touchTime(值)作为参数发送到changelab()函数,并显示在标签文本上


但是,我有一个NullReferenceException。我详细地签入了脚本。我如何修复这个问题?请告诉我我现在缺少什么。谢谢。

确保你有一个labelTest脚本连接到GameObject gol以使你的脚本正常工作,这是你现在获得null ref异常的唯一原因

但是,更进一步,不要在更新函数中保留这两行:

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);
    }
}
GameObject gol = GameObject.Find ("gol");
labelTest go = gol.GetComponent<labelTest>();
在脚本顶部。然后在inspector中,您可以将标签gameObject指定为此参数。

如果您必须在运行时分配它,那么请在“开始”而不是“更新”中进行分配

事实上,除非你对labelTest有进一步的计划,否则你可以把它全部扔掉,然后使用

public UILabel timeLabel;
在CheckTime和change的顶部

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


确保你有一个labelTest脚本连接到GameObject gol以使你的脚本正常工作,这是你现在将获得null ref异常的唯一原因

但是,更进一步,不要在更新函数中保留这两行:

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);
    }
}
GameObject gol = GameObject.Find ("gol");
labelTest go = gol.GetComponent<labelTest>();
在脚本顶部。然后在inspector中,您可以将标签gameObject指定为此参数。

如果您必须在运行时分配它,那么请在“开始”而不是“更新”中进行分配

事实上,除非你对labelTest有进一步的计划,否则你可以把它全部扔掉,然后使用

public UILabel timeLabel;
在CheckTime和change的顶部

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


确保你有一个labelTest脚本连接到GameObject gol以使你的脚本正常工作,这是你现在将获得null ref异常的唯一原因

但是,更进一步,不要在更新函数中保留这两行:

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);
    }
}
GameObject gol = GameObject.Find ("gol");
labelTest go = gol.GetComponent<labelTest>();
在脚本顶部。然后在inspector中,您可以将标签gameObject指定为此参数。

如果您必须在运行时分配它,那么请在“开始”而不是“更新”中进行分配

事实上,除非你对labelTest有进一步的计划,否则你可以把它全部扔掉,然后使用

public UILabel timeLabel;
在CheckTime和change的顶部

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


确保你有一个labelTest脚本连接到GameObject gol以使你的脚本正常工作,这是你现在将获得null ref异常的唯一原因

但是,更进一步,不要在更新函数中保留这两行:

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);
    }
}
GameObject gol = GameObject.Find ("gol");
labelTest go = gol.GetComponent<labelTest>();
在脚本顶部。然后在inspector中,您可以将标签gameObject指定为此参数。

如果您必须在运行时分配它,那么请在“开始”而不是“更新”中进行分配

事实上,除非你对labelTest有进一步的计划,否则你可以把它全部扔掉,然后使用

public UILabel timeLabel;
在CheckTime和change的顶部

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


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

在你的MyManager课上

public class MyManager : MonoBehaviour
{
    public static MyManager instance;

    void Awake() {
        instance = this;
    }

    public void MyFunction() {
        //funcion code
    }

}
然后,当您想从项目中的任何位置调用此函数时,只需调用

MyManager.instance.MyFunction()

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

在你的MyManager课上

public class MyManager : MonoBehaviour
{
    public static MyManager instance;

    void Awake() {
        instance = this;
    }

    public void MyFunction() {
        //funcion code
    }

}
然后,当您想从项目中的任何位置调用此函数时,只需调用

MyManager.instance.MyFunction()

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

在你的MyManager课上

public class MyManager : MonoBehaviour
{
    public static MyManager instance;

    void Awake() {
        instance = this;
    }

    public void MyFunction() {
        //funcion code
    }

}
然后,当您想从项目中的任何位置调用此函数时