c# - 我怎样才能永远销毁游戏对象?

标签 c# unity3d

所以,我有这个药水。当我的游戏中的玩家接触到药水时,我想销毁药水。但是,如果玩家死亡,场景将重新加载并且药水仍会在关卡中。如果玩家与药水发生碰撞,我不希望他们获得药水。他们应该只能收集一次药水。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class DestroyPotionForever : MonoBehaviour
{
    public bool potionCollide = false;

    // Start is called before the first frame update
    void Start()
    {
        
    }
    void OnTriggerEnter(){
        if(potionCollide == false){
            Destroy(gameObject);
            bool potionCollide = true;
        }
    }


    // Update is called once per frame
    void Update()
    {
        
    }
}

但是...此代码不起作用。感谢您的帮助。

最佳答案

一个简单的方法是存储你是否在 PlayerPrefs 中拿起药水。 .

然后你可以这样做:

void OnTriggerEnter()
{
    if(PlayerPrefs.GetInt("GotPotion", 0) == 0)
    {
        // You didn't get the potion yet, so get it
        Destroy(gameObject);
        PlayerPrefs.SetInt("GotPotion", 1); // got the potion
    }
}

那么无论你在哪里产生你的药水,你都可以:

if(PlayerPrefs.GetInt("GotPotion", 0) == 1)
{
    // Got the potion already, so don't spawn the potion
}

或者,如果你直接把药水放在场景中,你可以这样做:

void Start()
{
    if(PlayerPrefs.GetInt("GotPotion", 0) == 1)
    {
        // If the potion is already picked up, destroy it
        Destroy(gameObject);
    }
}

一个更好的方法是按照 Antnio Pedro Gonalves Ferreira 的建议编写您自己的保存系统,但这至少会让您通过演示阶段。

关于c# - 我怎样才能永远销毁游戏对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63385954/

相关文章:

unity3d - 团结 | mesh.colors 不会为我的自定义网格对象着色

oop - 与 OOP(或其他范式)相比,ECS(实体-组件-系统)架构模式有哪些缺点?

c# - XNA 新手,一般问题

c# - 使用 Microsoft.HttpClient 和 HttpContentExtensions 的通用 POST 请求

c# - 如何设置csi.exe的平台架构

c# - 如果连接已经打开,可以调用 OracleConnection.Open() 吗?

c# - 为什么需要 NativeArray 从 Unity 的作业系统获取返回值?

c# - 如何中止来自 .NET 的 HTTP 请求?

unity3d - UnusedBytecodeStripper2.exe 未正常运行

unity3d - Unity Canvas 中的损坏文本