2014-06-21 70 views
-4

我有从另一个脚本引用功能的问题。我真的不知道该怎么做。我寻找解决方案,但我不明白这一点。 有人可以说我做错了什么吗?从另一个脚本引用功能

第一个脚本[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(); 

       } 


      } 
     } 

    } 
     } 
    } 

另一个脚本[obrot.cs]

using UnityEngine; 
    using System.Collections; 

public class obrot : MonoBehaviour { 
public float speed = 5f; 


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

    obr(); 


} 

void obr() 
{ 
    transform.Translate(new Vector3(1,0,0) * speed * Time.deltaTime); 
} 
    } 

回答

0

为了从另一个调用脚本的功能,你需要有到GameObject或参考附加到该GameObject的脚本Component

如果你有参考游戏物体,你可以简单地使用SendMessage这样的:

gameObjReference.SendMessage("NameOfFunction"); 

如果你有参考脚本,你可以这样调用该函数:

scriptReference.FunctionName(); 

然而,你的大问题将是,你如何得到这些参考。

GameObject gameObjReference = GameObject.Find("Name of GameObject in Scene"); 

也就是说,你把GameObject的名字,你在场景中称之为什么。

其他方法是类似的。

ScriptName scriptReference = GameObject.Find("Name of GameObject").GetComponent<ScriptName>();