在编辑器中分配脚本而不是GameObject?

时间:2015-04-24 19:22:00

标签: c# unity3d

我通过在编辑器中指定它们来连接我的GameObjects。

我需要的只是分配给GameObject的脚本,但我总是要这样做:

public class GameBoard : MonoBehaviour {
    //assigned the gameobject containing the ScoreScript in the editor
    public GameObject totalScore;  
    private ScoreScript totalScoreScript;

    void Start() {
        totalScoreScript = this.GetComponent<ScoreScript>();
    }
}

有更好的方法来实现这一结果吗?

1 个答案:

答案 0 :(得分:2)

只需使用ScoreScript作为公共变量。要指定它,请通过编辑器正常连接脚本来拖动游戏对象。这也适用于像Transform这样的内置组件。

public class GameBoard : MonoBehaviour {
    public ScoreScript totalScoreScript;
}