第一种,被调用脚本函数为static类型,调用时直接用 脚本名.函数名()
第二种,GameObject.Find("脚本所在的物体的名字").SendMessage("函数名"); //能调用public和private类型函数
第三种,GameObject.Find("脚本所在的物体的名字").GetComponent<脚本名>().函数名(); //只能调用public类型函数
例子,飞机大战中的跨脚本调用自己的飞机消失后重新开始的方法:
using UnityEngine;
using System.Collections;
public class move : MonoBehaviour {
public float xMin, xMax, zMin, zMax;
public float startTime;
private float CountTime;
public GameObject zidan1;
GameController g;
// Use this for initialization
void Start() {
CountTime = startTime;
g = GameObject.Find("GameObject").GetComponent<GameController>();
}
// Update is called once per frame
void Update() {
}
}
private void OnTriggerEnter(Collider other) {
if (other.tag != "Player") {
Destroy(other.gameObject);
Destroy(this.gameObject);
g.JiaZai();
}
}
}