Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.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#_Function_Reference_Unity3d - Fatal编程技术网

C# 从另一个脚本引用函数

C# 从另一个脚本引用函数,c#,function,reference,unity3d,C#,Function,Reference,Unity3d,我无法从另一个脚本引用函数。我真的不知道该怎么办。我寻找解决办法,但我不明白。 有人能说我做错了什么吗 第一个脚本[dotykxd.cs]: using UnityEngine; using System.Collections; public class dotykxd : MonoBehaviour { void Start() { } // Use this for initialization // Update is called once per fram

我无法从另一个脚本引用函数。我真的不知道该怎么办。我寻找解决办法,但我不明白。 有人能说我做错了什么吗

第一个脚本[dotykxd.cs]:

using UnityEngine;
using System.Collections;

 public class dotykxd : MonoBehaviour {


void Start()
{




    }
// Use this for initialization


// Update is called once per frame
void Update () {

    if(Input.touches.Length <=0)
    {

    }

    else{

        for(int i=0; i< Input.touchCount;i++)
        {
            if(this.guiTexture.HitTest(Input.GetTouch(i).position))
            {/*
                if(Input.GetTouch(i).phase == TouchPhase.Began)
                {
                    Camera.main.backgroundColor=Color.black;
                }*/

                if(Input.GetTouch(i).phase == TouchPhase.Ended)
                {
                    obrot obrotek= GetComponents<obrot> ();
                    obrot.obr();

                }


            }
        }

    }
      }
  }

要从另一个脚本调用函数,您需要有对该脚本的引用或附加到该游戏对象的脚本

如果您有对GameObject的引用,您可以这样简单地使用:

gameObjReference.SendMessage("NameOfFunction"); 
scriptReference.FunctionName();
如果您有对脚本的引用,您可以像这样调用函数:

gameObjReference.SendMessage("NameOfFunction"); 
scriptReference.FunctionName();

然而,你更大的问题是,你如何获得这些参考资料

GameObject gameObjReference = GameObject.Find("Name of GameObject in Scene");
也就是说,你把游戏对象的名字,你在场景中对它的称呼

另一种方法类似

ScriptName scriptReference = GameObject.Find("Name of GameObject").GetComponent<ScriptName>();
ScriptName scriptReference=GameObject.Find(“GameObject的名称”).GetComponent();