[英]How can I Transfer my Score Game Object from my game scene to game over scene in Unity
So i have my game scene and i want to have my score be displayed in game over scene and my high score.所以我有我的游戏场景,我想让我的分数显示在游戏结束场景和我的高分中。 This is my score code:
这是我的分数代码:
public class Score : MonoBehaviour
{
public Text scoreText;
public float scoreAmount;
public float pointIncreasedPerSecond;
// Start is called before the first frame update
void Start()
{
scoreAmount = 0f;
pointIncreasedPerSecond = 1f;
}
// Update is called once per frame
void Update()
{
scoreText.text = (int)scoreAmount + "";
scoreAmount +=pointIncreasedPerSecond * Time.deltaTime;
}
}
Unity has a DontDestroyOnLoad
method that will keep any objects alive across scene loads. Unity 有一个
DontDestroyOnLoad
方法,可以让任何对象在场景加载时保持活动状态。 You can add a call to that in your Score
script.您可以在
Score
脚本中添加对它的调用。
void Awake() {
DontDestroyOnLoad(gameObject);
}
Note that the GameObject must be a "root" GameObject
(meaning it doesn't have parents in the scene).请注意,游戏对象必须是“根”
GameObject
(意味着它在场景中没有父对象)。 Also, be careful to not keep anything extra that you don't want alive around with it (child GameObjects
will be left intact).此外,请注意不要保留任何您不希望在其周围存活的额外内容(子
GameObjects
将保持原样)。 The linked documentation states:链接的文档指出:
If the target Object is a component or GameObject, Unity also preserves all of the Transform's children.
如果目标 Object 是组件或游戏对象,Unity 还会保留 Transform 的所有子对象。 Object.DontDestroyOnLoad only works for root GameObjects or components on root GameObjects.
Object.DontDestroyOnLoad 仅适用于根 GameObjects 或根 GameObjects 上的组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.