`

unity基础开发----同一个场景不同物体传值问题

 
阅读更多

同一个场景在不同的物体之间进行传值,就像,在cube上有一个得分的脚本,我Main Camera上一个脚本需要用到cube上的得分。

using UnityEngine;
using System.Collections;

public class Score : MonoBehaviour {

    public  int allScore=100;


	// Update is called once per frame
	void Update () {
        allScore = 100;
	
	}
}

这个脚本放在cube上,我想调用这个allScore。

using UnityEngine;
using System.Collections;

public class text : MonoBehaviour {


    public GameObject Obj1;
    
	// Use this for initialization
	void Start () {
        Obj1 = GameObject.Find("Cube");
        Score script=Obj1.GetComponent<Score>();
        Debug.Log(script.allScore);	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

这样就可以打印出allScore了。很简单但是很实用。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics