[英]How to collect gold coin only once in a level
I have some levels in my game.我的游戏中有一些关卡。 Some of the levels contain a gold coin.一些关卡包含金币。 I need the player to be able to collect it only once.我需要玩家只能收集一次。 If the user plays the same level again the coin should not appear again.如果用户再次玩相同的级别,硬币不应再次出现。 (I'm using unity and c#) (我正在使用统一和 C#)
Here the Script attached to the coin:这里附在硬币上的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Goldcoin : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
SaveManager.Instance.state.goldcoin++;
SaveManager.Instance.Save();
Destroy(gameObject);
}
}
}
What you want is data persistance between scenes.您想要的是场景之间的数据持久性。
Data persistence means that even if you close the app, or change the scene, this data will persist, won't be deleted. 数据持久化意味着即使你关闭应用,或者改变场景,这些数据也会持久化,不会被删除。
If you want persistance only in the same program execution, you can have an object that won't be destroyed on every Scene load, using DontDestroyOnLoad .如果您只想在相同的程序执行中保持持久性,您可以使用 DontDestroyOnLoad 拥有一个不会在每次场景加载时都被销毁的object 。
But if you want to keep this information between multiple executions of the app you need something else, like a file.但是,如果您想在应用程序的多次执行之间保留此信息,则需要其他内容,例如文件。 You can store the state of the current scene (or even the whole game) in a multiple ways, the easyiest one is:您可以通过多种方式存储当前场景(甚至整个游戏)的state,最简单的一种是:
If you are not familiar with JSON's, you can check my JsonManager Repo.如果您不熟悉 JSON,可以查看我的JsonManager Repo。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.