Coin Persist 脚本无法正常工作 [英] Coin Persist script not working correctly

查看:34
本文介绍了Coin Persist 脚本无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个带有硬币拾取器的 2D 平台游戏,一切正常,但是当您进入下一个级别时,该级别的硬币拾取器将无法正确加载.

I am creating a 2D platformer game with coin pickups, everything works fine, but when you proceed to the next level the coin pickups of that level won't load correctly.

我有一个带有 screenPersist 脚本的 screen Persist 游戏对象,然后我有一个带有 coinPickup 脚本的拾取物(硬币).

I have a screen Persist game object with a screenPersist script, then I have my pickups (coins), with a coinPickup script.

唯一的问题是当您进入下一个级别时,该级别的硬币不会加载.分数和其他一切正常.

The only problem is when you proceed to the next level the coins of that level won't load. the score and everything else works fine.

以下是我的 scipts 示例.

Here are examples of my scipts.

  1. screenPesist 脚本.

  1. screenPesist script.

 public class ScreenPersist : MonoBehaviour
 {
     int startingSceneIndex;

     private void Awake()
     {
         int numScenePersist = FindObjectsOfType<ScreenPersist>().Length;

         if (numScenePersist > 1)
         {
             Destroy(gameObject);
         } else
         {
             DontDestroyOnLoad(gameObject);
         }
     }

     // Start is called before the first frame update
     void Start()
     {
         startingSceneIndex = SceneManager.GetActiveScene().buildIndex;
     }

     // Update is called once per frame
     void Update()
     {
         int currentScenIndex = SceneManager.GetActiveScene().buildIndex;

         if (currentScenIndex != startingSceneIndex)
         {
             Destroy(gameObject);
         }
     }
 }

  • 硬币拾取脚本.

  • Coin pickup Script.

    public class CoinPickup : MonoBehaviour
    {
        [SerializeField] AudioClip coinPickupSFX;
        [SerializeField] int piontsForCoinPickup;
    
         private void OnTriggerEnter2D(Collider2D collision)
         {
             FindObjectOfType<GameSesion>().addToScore(piontsForCoinPickup);
             AudioSource.PlayClipAtPoint(coinPickupSFX, 
             Camera.main.transform.position);
             Destroy(gameObject);
         }
    }
    

  • 推荐答案

    如果您只是检查计数并删除,您目前无法控制 ScreenPersist 游戏对象的哪个实例被销毁.维护 DontDestroyOnLoad() 对象的单个实例的正确方法是这样的:

    You currently have no control over which instance of the ScreenPersist GameObject gets destroyed, if you simply check the count and delete. The correct way to maintain a single instance of a DontDestroyOnLoad() object is like this:

     public static ScreenPersist m_screenPersist;
    private void Awake()
    {
        DontDestroyOnLoad(gameObject);
        if (m_screenPersist == null) m_screenPersist = this;
        else Destroy(gameObject);
    }
    

    理想情况下,您不希望将硬币拾取器作为 ScreenPersist 游戏对象的子项.最好有一个游戏管理器更优雅地处理它,最后将您需要保留的任何数据写入 ScreenPersist 类,或写入可以用作 DontDestroyOnLoad GameObject 的同一个 GameManager 类

    Ideally, you wouldnt want to have the coin pickups as a child of the ScreenPersist GameObject. Better to have a game manager that takes care of it more elegantly, and finally writes any data that you need to persist, into the ScreenPersist class, or into the same GameManager class which can function as a DontDestroyOnLoad GameObject

    这篇关于Coin Persist 脚本无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆