脚本1:
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { // Use this for initialization void Start () { ScriptB b = null; b = GameObject.Find ("Main Camera").GetComponent<ScriptB> (); Debug.Log ("ScriptB‘s num_b = " + b.num_b); } // Update is called once per frame void Update () { } }
脚本2:
using UnityEngine; using System.Collections; public class ScriptB : MonoBehaviour { public int num_b = 5; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } }
把脚本1脚本2都添加到Main Camera中,可以看到如下运行结果,脚本1成功调用了脚本2中的数据:
Unity 调用另一个脚本数据
时间: 2024-11-06 09:52:14