gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:31:48 24 4
gpt4 key购买 nike

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

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

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

关于c# - 在编辑器中分配脚本而不是游戏对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29855789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com