在编辑器中分配脚本而不是游戏对象?

Assign script instead of GameObject in editor?

我正在通过在编辑器中分配它们来连接我的游戏对象。

我只需要分配给游戏对象的脚本,但我总是必须这样做:

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>();
    }
}

有没有更好的方法可以达到这个效果?

只需使用 ScoreScript 作为 public 变量。要分配它,请通过编辑器正常拖动带有脚本的游戏对象。这也适用于 Transform 等内置组件。

public class GameBoard : MonoBehaviour {
    public ScoreScript totalScoreScript;
}