Forum begins after the advertisement:


[Part 8] How to let each health and mana orbs call once only in the game

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 8] How to let each health and mana orbs call once only in the game

Viewing 15 posts - 1 through 15 (of 46 total)
  • Author
    Posts
  • #14959
    Elvin Sim
    Participant
    #14960
    Elvin Sim
    Participant
    Helpful?
    Up
    0
    ::
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class IncreaseMaxHealth : MonoBehaviour
    {
        [SerializeField] GameObject particles;
        [SerializeField] GameObject canvasUI;
    
        [SerializeField] HeartShards heartShards;
    
        bool used;
    
        // Start is called before the first frame update
        void Start()
        {
            if (PlayerController.Instance.maxHealth >= PlayerController.Instance.maxTotalHealth)
            {
                Destroy(gameObject);
            }
        }
    
        private void OnTriggerEnter2D(Collider2D _collision)
        {
            if (_collision.CompareTag("Player") && !used)
            {
                used = true;
                StartCoroutine(ShowUI());
            }
        }
    
        IEnumerator ShowUI()
        {
            GameObject _particles = Instantiate(particles, transform.position, Quaternion.identity);
            Destroy(_particles, 0.5f);
            yield return new WaitForSeconds(0.5f);
    
            canvasUI.SetActive(true);
            heartShards.initialFillAmount = PlayerController.Instance.heartShards * 0.25f;
            PlayerController.Instance.heartShards++;
            heartShards.targetFillAmount = PlayerController.Instance.heartShards * 0.25f;
    
            StartCoroutine(heartShards.LerpFill());
    
            yield return new WaitForSeconds(2.5f);
            SaveData.Instance.SavePlayerData();
            canvasUI.SetActive(false);
            Destroy(gameObject);
        }
    }
    #14961
    Elvin Sim
    Participant
    Helpful?
    Up
    0
    ::
    public class AddManaOrb : MonoBehaviour
    {
        [SerializeField] GameObject particles;
        [SerializeField] GameObject canvasUI;
    
        [SerializeField] OrbShard orbShard;
    
        bool used;
    
        // Start is called before the first frame update
        void Start()
        {
            if (PlayerController.Instance.manaOrbs >= 3)
            {
                Destroy(gameObject);
            }
        }
    
        private void OnTriggerEnter2D(Collider2D _collision)
        {
            if (_collision.CompareTag("Player") && !used)
            {
                used = true;
                StartCoroutine(ShowUI());
            }
        }
    
        IEnumerator ShowUI()
        {
            GameObject _particles = Instantiate(particles, transform.position, Quaternion.identity);
            Destroy(_particles, 0.5f);
            yield return new WaitForSeconds(0.5f);
    
            canvasUI.SetActive(true);
            orbShard.initialFillAmount = PlayerController.Instance.orbShard * 0.34f;
            PlayerController.Instance.orbShard++;
            orbShard.targetFillAmount = PlayerController.Instance.orbShard * 0.34f;
    
            StartCoroutine(orbShard.LerpFill());
    
            yield return new WaitForSeconds(2.5f);
            SaveData.Instance.SavePlayerData();
            canvasUI.SetActive(false);
            Destroy(gameObject);
        }
    }
    #14969
    Joseph Tang
    Moderator
    Helpful?
    Up
    0
    ::

    I’ve addressed this in another topic but a TL;DR, it will need to be an entirely new mechanic, and depending on your usage of it, you it may be laborious.

    [Part 8] MaxHealth increases from heart shards not saved

    You can check if the method in point [2] is sufficient.

    #14984
    Elvin Sim
    Participant
    Helpful?
    Up
    0
    ::

    I would like to ask for HashSet I need to write like this HashSet<string>(), right?

    Can I know what these mean?
    Preparation:
    1) Tags: Create a “Pickup” and “Chest” tag and set them on the related gameobjects.

    2) A way to call GameManager’s TriggerChestInteract() or TriggerPickupInteract(). This will primarily be from scripts, you can add a line into, let’s say, orbShard or heartShard scripts that when picked up, they call the GameManager and give the gameobject’s name. For instance: GameManager.Instance.TriggerPickupInteract(gameObject.name).

    3) Create a null object and set the pickupmanager script onto it.

    #14985
    Elvin Sim
    Participant
    Helpful?
    Up
    0
    ::

    Like “Pickup” and “Chest” are the same or they are different things, Because I have orbs to pickup yes, but don’t have chest to open and get it

    #14986
    Joseph Tang
    Moderator
    Helpful?
    Up
    0
    ::

    Yes, when creating a hashset variable, you should type it out as HashSet<string> name, without the regular brackets.

    As for when actually making the HashSet, use HashSet().


    1. Tags

    On every game object in the inspector, you can see an option on the top left. Most things will have it say “Untagged”.
    This is where you set a tag, for this step, you should create 2 tags which you can get to by selecting the “Untagged” box and picking the last option.

    In this case, chest and pickup are 2 different things, but they are also only examples. Meaning, you dont need both. just have the tag that you need for your game.


    2. Calling GameManager

    This step simply means that if you want the code in GameManager.cs to function, you first need to call it like any method.
    So for the purpose of the mechanic, you should put it in your pickups or chests gameobject scripts.

    In those scripts, call the method created from GameManager.Instance.


    3. Null / Empty GameObject

    Right click your scene or hierarchy, then select null object/create new game object.

    Add the new pickupmanager.cs script created in the post to this new gameobject.

    #14987
    Elvin Sim
    Participant
    Helpful?
    Up
    0
    ::

    okok thank you for your answer!, I will try it and tell you the result

    #14988
    Elvin Sim
    Participant
    #14989
    Elvin Sim
    Participant
    Helpful?
    Up
    0
    ::

    Still cannot works

    #14990
    Elvin Sim
    Participant
    Helpful?
    Up
    0
    ::
    public struct SaveData
    {
        public static SaveData Instance;
    
        //map stuff
        public HashSet<string> sceneNames;
    
        //bench stuff
        public string benchSceneName;
        public Vector2 benchPos;
    
        //player stuff
        public int playerHealth;
        public int playerMaxHealth;
        public int playerHeartShards;
        public float playerMana;
        public int playerManaOrbs;
        public int playerOrbShard;
        public float playerOrb0fill, playerOrb1fill, playerOrb2fill;
        public bool playerHalfMana;
        public Vector2 playerPosition;
        public string lastScene;
    
        public bool playerUnlockedWallJump, playerUnlockedDash, playerUnlockedVarJump;
        public bool playerUnlockedSideCast, playerUnlockedUpCast, playerUnlockedDownCast;
       
        //enemies stuff
        //shade
        public Vector2 shadePos;
        public string sceneWithShade;
        public Quaternion shadeRot;
    
        //The Hollow Knight
        public bool THKDefeated;
    
        // Pickups and Chests data
        public HashSet<string> collectedPickups; // HashSet to store collected pickups
    
        public void Initialize()
        {
            if(!File.Exists(Application.persistentDataPath + "/save.scenes.data"))
            {
                using (BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.scenes.data")))
                {
                    writer.Write(0); // Write an empty scene data structure
                }
            }
    
            // Load existing data
            LoadSceneData();
    
            if (!File.Exists(Application.persistentDataPath + "/save.bench.data")) 
            {
                BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.bench.data"));
            }
    
            if (!File.Exists(Application.persistentDataPath + "/save.player.data"))
            {
                BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.player.data"));
            }
    
            if (!File.Exists(Application.persistentDataPath + "/save.shade.data"))
            {
                BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.shade.data"));
            }
    
            if (!File.Exists(Application.persistentDataPath + "/save.boss.data"))
            {
                BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.boss.data"));
            }
    
            if (!File.Exists(Application.persistentDataPath + "/save.pickups.data"))
            {
                BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.pickups.data"));
            }
    
            if (sceneNames == null)
            {
                sceneNames = new HashSet<string>();
            }
    
            if (collectedPickups == null)
            {
                collectedPickups = new HashSet<string>();
            }
        }
    
        #region Scenes Stuff
        public void SaveSceneData()
        {
            using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.scenes.data")))
            {
                writer.Write(sceneNames.Count);
                foreach (string sceneName in sceneNames)
                {
                    writer.Write(sceneName);
                }
            }
        }
    
        public void LoadSceneData()
        {
            if (File.Exists(Application.persistentDataPath + "/save.scenes.data"))
            {
                using (BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.scenes.data")))
                {
                    int sceneCount = reader.ReadInt32();
                    sceneNames = new HashSet<string>();
                    for (int i = 0; i < sceneCount; i++)
                    {
                        string sceneName = reader.ReadString();
                        sceneNames.Add(sceneName);
                    }
                }
            }
            else
            {
                sceneNames = new HashSet<string>();
            }
        }
        #endregion
    
        #region Delete save data
        public void DeleteScenesData()
        {
            string path = Application.persistentDataPath + "/save.scenes.data";
            if (File.Exists(path))
            {
                File.Delete(path);
                Debug.Log("Scenes data deleted.");
            }
        }
    
        public void DeleteBenchData()
        {
            string path = Application.persistentDataPath + "/save.bench.data";
            if (File.Exists(path))
            {
                File.Delete(path);
                Debug.Log("Bench data deleted.");
            }
        }
    
        public void DeletePlayerData()
        {
            string path = Application.persistentDataPath + "/save.player.data";
            if (File.Exists(path))
            {
                File.Delete(path);
                Debug.Log("Player save data deleted.");
            }
        }
    
        public void DeleteShadeData()
        {
            string path = Application.persistentDataPath + "/save.shade.data";
            if (File.Exists(path))
            {
                File.Delete(path);
                Debug.Log("Shade save data deleted.");
            }
        }
    
        public void DeleteBossData()
        {
            string path = Application.persistentDataPath + "/save.boss.data";
            if (File.Exists(path))
            {
                File.Delete(path);
                Debug.Log("Boss save data deleted.");
            }
        }
    
        public void DeleteAllSaveData()
        {
            DeleteScenesData();
            DeleteBenchData();
            DeletePlayerData();
            DeleteShadeData();
            DeleteBossData();
        }
    
        #endregion
    
        #region Bench Stuff
        public void SaveBench()
        {
            using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.bench.data")))
            {
                writer.Write(benchSceneName);
                writer.Write(benchPos.x);
                writer.Write(benchPos.y);
            }
        }
    
        public void LoadBench()
        {
            string savePath = Application.persistentDataPath + "/save.bench.data";
            if (File.Exists(savePath) && new FileInfo(savePath).Length > 0)
            {
                using(BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.bench.data")))
                {
                    benchSceneName = reader.ReadString();
                    benchPos.x = reader.ReadSingle();
                    benchPos.y = reader.ReadSingle();
                }
            }
            else
            {
                Debug.Log("Bench doesn't exist");
            }
        }
        #endregion
    
        #region Player Stuff
        public void SavePlayerData()
        {
            using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.player.data")))
            {
                playerHealth = PlayerController.Instance.Health;
                writer.Write(playerHealth);
                playerMaxHealth = PlayerController.Instance.maxHealth;
                writer.Write(playerMaxHealth);
                playerHeartShards = PlayerController.Instance.heartShards;
                writer.Write(playerHeartShards);
    
                playerMana = PlayerController.Instance.Mana;
                writer.Write(playerMana);
                playerHalfMana = PlayerController.Instance.halfMana;
                writer.Write(playerHalfMana);
                playerManaOrbs = PlayerController.Instance.manaOrbs;
                writer.Write(playerManaOrbs);
                playerOrbShard = PlayerController.Instance.orbShard;
                writer.Write(playerOrbShard);
                playerOrb0fill = PlayerController.Instance.manaOrbsHandler.orbFills[0].fillAmount;
                writer.Write(playerOrb0fill);
                playerOrb1fill = PlayerController.Instance.manaOrbsHandler.orbFills[1].fillAmount;
                writer.Write(playerOrb1fill);
                playerOrb2fill = PlayerController.Instance.manaOrbsHandler.orbFills[2].fillAmount;
                writer.Write(playerOrb2fill);
    
                playerUnlockedWallJump = PlayerController.Instance.unlockedWallJump;
                writer.Write(playerUnlockedWallJump);
                playerUnlockedDash = PlayerController.Instance.unlockedDash;
                writer.Write(playerUnlockedDash);
                playerUnlockedVarJump = PlayerController.Instance.unlockedVarJump;
                writer.Write(playerUnlockedVarJump);
    
                playerUnlockedSideCast = PlayerController.Instance.unlockedSideCast;
                writer.Write(playerUnlockedSideCast);
                playerUnlockedUpCast = PlayerController.Instance.unlockedUpCast;
                writer.Write(playerUnlockedUpCast);
                playerUnlockedDownCast = PlayerController.Instance.unlockedDownCast;
                writer.Write(playerUnlockedDownCast);
                
    
                playerPosition = PlayerController.Instance.transform.position;
                writer.Write(playerPosition.x);
                writer.Write(playerPosition.y);
    
                lastScene = SceneManager.GetActiveScene().name;
                writer.Write(lastScene);
    
            }
            Debug.Log("saved player data");
        }
    
        public void LoadPlayerData()
        {
            string savePath = Application.persistentDataPath + "/save.player.data";
            if (File.Exists(savePath) && new FileInfo(savePath).Length > 0)
            {
                using(BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.player.data")))
                {
                    playerHealth = reader.ReadInt32();
                    playerMaxHealth = reader.ReadInt32();
                    playerHeartShards = reader.ReadInt32();
                    playerMana = reader.ReadSingle();
                    playerHalfMana = reader.ReadBoolean();
                    playerManaOrbs = reader.ReadInt32();
                    playerOrbShard = reader.ReadInt32();
                    playerOrb0fill = reader.ReadSingle();
                    playerOrb1fill = reader.ReadSingle();
                    playerOrb2fill = reader.ReadSingle();
    
                    playerUnlockedWallJump = reader.ReadBoolean();
                    playerUnlockedDash= reader.ReadBoolean();
                    playerUnlockedVarJump= reader.ReadBoolean();
    
                    playerUnlockedSideCast= reader.ReadBoolean();
                    playerUnlockedUpCast = reader.ReadBoolean();
                    playerUnlockedDownCast = reader.ReadBoolean();
    
                    playerPosition.x = reader.ReadSingle();
                    playerPosition.y = reader.ReadSingle();
    
                    lastScene = reader.ReadString();
    
                    SceneManager.LoadScene(lastScene);
                    PlayerController.Instance.transform.position = playerPosition;
                    PlayerController.Instance.halfMana = playerHalfMana;
                    PlayerController.Instance.Health = playerHealth;
                    PlayerController.Instance.maxHealth = playerMaxHealth;
                    PlayerController.Instance.heartShards = playerHeartShards;
                    PlayerController.Instance.Mana = playerMana;
                    PlayerController.Instance.manaOrbs = playerManaOrbs;
                    PlayerController.Instance.orbShard = playerOrbShard;
                    PlayerController.Instance.manaOrbsHandler.orbFills[0].fillAmount = playerOrb0fill;
                    PlayerController.Instance.manaOrbsHandler.orbFills[1].fillAmount = playerOrb1fill;
                    PlayerController.Instance.manaOrbsHandler.orbFills[2].fillAmount = playerOrb2fill;
    
                    PlayerController.Instance.unlockedWallJump = playerUnlockedWallJump;
                    PlayerController.Instance.unlockedDash = playerUnlockedDash;
                    PlayerController.Instance.unlockedVarJump = playerUnlockedVarJump;
    
                    PlayerController.Instance.unlockedSideCast = playerUnlockedSideCast;
                    PlayerController.Instance.unlockedUpCast = playerUnlockedUpCast;
                    PlayerController.Instance.unlockedDownCast = playerUnlockedDownCast;
    
                }
                Debug.Log("load player data");
                Debug.Log(playerHalfMana);
            }
            else
            {
                Debug.Log("File doesn't exist");
                PlayerController.Instance.halfMana = false;
                PlayerController.Instance.Health = PlayerController.Instance.maxHealth;
                PlayerController.Instance.Mana = 0.5f;
                PlayerController.Instance.heartShards = 0;
    
                PlayerController.Instance.unlockedWallJump = false;
                PlayerController.Instance.unlockedDash = false;
                PlayerController.Instance.unlockedVarJump = false;
    
                PlayerController.Instance.unlockedSideCast = false;
                PlayerController.Instance.unlockedUpCast = false;
                PlayerController.Instance.unlockedDownCast = false;
    
            }
        }
        #endregion
    
        #region Shade Stuff
        public void SaveShadeData()
        {
            using(BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.shade.data")))
            {
                sceneWithShade = SceneManager.GetActiveScene().name;
                shadePos = Shade.Instance.transform.position;
                shadeRot = Shade.Instance.transform.rotation;
    
                writer.Write(sceneWithShade);
    
                writer.Write(shadePos.x);
                writer.Write(shadePos.y);
    
                writer.Write(shadeRot.x);
                writer.Write(shadeRot.y);
                writer.Write(shadeRot.z);
                writer.Write(shadeRot.w);
            }
        }
    
        public void LoadShadeData()
        {
            string savePath = Application.persistentDataPath + "/save.shade.data";
            if (File.Exists(savePath) && new FileInfo(savePath).Length > 0)
            {
                using(BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.shade.data")))
                {
                    sceneWithShade = reader.ReadString();
                    shadePos.x = reader.ReadSingle();
                    shadePos.y = reader.ReadSingle();
    
                    float rotationX = reader.ReadSingle();
                    float rotationY = reader.ReadSingle();
                    float rotationZ = reader.ReadSingle();
                    float rotationW = reader.ReadSingle();
                    shadeRot = new Quaternion(rotationX, rotationY, rotationZ, rotationW);
                }
                Debug.Log("Load shade data");
            }
            else
            {
                Debug.Log("Shade doesn't exist");
            }
        }
        #endregion
    
        #region The Hollow Knight
        public void SaveBossData()
        {
            using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.boss.data")))
            {
                THKDefeated = GameManager.Instance.THKDefeated;
    
                writer.Write(THKDefeated);
            }
        }
    
        public void LoadBossData()
        {
            string savePath = Application.persistentDataPath + "/save.boss.data";
            if (File.Exists(savePath) && new FileInfo(savePath).Length > 0)
            {
                using (BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.boss.data")))
                {
                    THKDefeated = reader.ReadBoolean();
    
                    GameManager.Instance.THKDefeated = THKDefeated;
                }
            }
            else
            {
                Debug.Log("Boss doesnt exist");
            }
        }
        #endregion
    
     
        #region Orbs Save Data
        public void AddCollectedPickup(string pickupID)
        {
            // Add pickup ID to collected pickups
            collectedPickups.Add(pickupID);
            SavePickups();
        }
    
        public void SavePickups()
        {
            using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.pickups.data")))
            {
                // Save collected pickups
                writer.Write(collectedPickups.Count);
                foreach (string pickupID in collectedPickups)
                {
                    writer.Write(pickupID);
                }
            }
        }
    
        public void LoadPickups()
        {
            string savePath = Application.persistentDataPath + "/save.pickups.data";
            if (File.Exists(savePath) && new FileInfo(savePath).Length > 0)
            {
                using (BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.pickups.data")))
                {
                    // Load collected pickups
                    int numCollectedPickups = reader.ReadInt32();
                    for (int i = 0; i < numCollectedPickups; i++)
                    {
                        collectedPickups.Add(reader.ReadString());
                    }
                }
            }
        }
        #endregion
    }
    #14991
    Elvin Sim
    Participant
    Helpful?
    Up
    0
    ::
    public class PickupManager : MonoBehaviour
    {
        public static PickupManager Instance;
    
        private void Awake()
        {
            if(Instance != null && Instance != this)
            {
                Destroy(gameObject);
            }
            else
            {
                Instance = this;
            }
    
            DontDestroyOnLoad(gameObject);
        }
    
        // Start is called before the first frame update
        void Start()
        {
            LoadAndDeactivateInteractedObjects(); // Load saved data and deactivate pickups
        }
    
        public void LoadAndDeactivateInteractedObjects()
        {
            HashSet<string> collectedPickups = SaveData.Instance.collectedPickups;
    
            // Deactivate pickups that have been collected
            foreach (GameObject pickup in GameObject.FindGameObjectsWithTag("Pickup"))
            {
                string pickupID = pickup.name; // Assuming the name of the object is its ID
                if (collectedPickups.Contains(pickupID))
                {
                    pickup.SetActive(false); // Deactivate the pickup
                }
            }
        }
    }
    
    #14992
    Elvin Sim
    Participant
    Helpful?
    Up
    0
    ::
    public class GameManager : MonoBehaviour
    {
        public string transitionedFromScene;
    
        public Vector2 platformingRespawnPoint;
        public Vector2 respawnPoint;
        [SerializeField] Bench bench;
    
        public GameObject shade;
    
        public bool THKDefeated = false;
    
        [SerializeField] private FadeUI pauseMenu;
        [SerializeField] private float fadeTime;
        public bool gameIsPaused;
    
        public static GameManager Instance {  get; private set; }
        private void Awake()
        {
            SaveData.Instance.Initialize();
            if(Instance != null && Instance != this)
            {
                Destroy(gameObject);
            }
            else
            {
                Instance = this;
            }
    
            if(PlayerController.Instance != null)
            {
                if (PlayerController.Instance.halfMana)
                {
                    SaveData.Instance.LoadShadeData();
                    if(SaveData.Instance.sceneWithShade == SceneManager.GetActiveScene().name || SaveData.Instance.sceneWithShade == "")
                    {
                        Instantiate(shade, SaveData.Instance.shadePos, SaveData.Instance.shadeRot);
                    }
                }
            }
    
            SaveScene();
            DontDestroyOnLoad(gameObject);
            bench = FindObjectOfType<Bench>();
    
            SaveData.Instance.LoadBossData();
        }
    
        public void ResetData()
        {
            SaveData.Instance.DeleteAllSaveData();
        }
    
        private void Update()
        {
            if(Input.GetKeyDown(KeyCode.Escape) && !gameIsPaused)
            {
                pauseMenu.FadeUIIn(fadeTime);
                Time.timeScale = 0;
                gameIsPaused = true;
            }
        }
    
        public void UnpauseGame()
        {
            Time.timeScale = 1;
            gameIsPaused = false;
        }
    
        public void SaveScene()
        {
            string currentSceneName = SceneManager.GetActiveScene().name;
            SaveData.Instance.sceneNames.Add(currentSceneName);
            SaveData.Instance.SaveSceneData(); // Save scene names whenever a new scene is added
        }
    
        public void RespawnPlayer()
        {
            SaveData.Instance.LoadBench();
            if (SaveData.Instance.benchSceneName != null) //load the bench's scene if it exists.
            {
                SceneManager.LoadScene(SaveData.Instance.benchSceneName);
            }
    
            if(SaveData.Instance.benchPos != null) //set the respawn point to the bench's position.
            {
                respawnPoint = SaveData.Instance.benchPos;
            }
            else
            {
                respawnPoint = platformingRespawnPoint;
            }
    
            PlayerController.Instance.transform.position = respawnPoint;
    
            StartCoroutine(UIManager.Instance.DeactivateDeathScreen());
            PlayerController.Instance.Respawned();
        }
    
        
        // Method to trigger pickup interaction event
        public void TriggerPickupInteract(string pickupID)
        {
            SaveData.Instance.AddCollectedPickup(pickupID); // Add pickup to SaveData
        }
    }
    #14993
    Elvin Sim
    Participant
    Helpful?
    Up
    0
    ::

    PlayerController.cs

    public IEnumerator WalkIntoNewScene(Vector2 _exitDir, float _delay)
    {
        PickupManager.Instance.LoadAndDeactivateInteractedObjects();
        pState.invincible = true;
    
        //If exit direction is upwards
        if (_exitDir.y != 0)
        {
            rb.velocity = jumpForce * _exitDir;
        }
    
        //If exit direction requires horizontal movement
        if(_exitDir.x != 0)
        {
            xAxis = _exitDir.x > 0 ? 1 : -1;
    
            Move();
        }
    
        Flip();
        yield return new WaitForSeconds(_delay);
        pState.invincible = false;
        pState.cutscene = false;
    }
    #14994
    Joseph Tang
    Moderator
    Helpful?
    Up
    0
    ::

    From here, my help will not be as clear since this is an untested code / chatgpt code.

    First let’s check if the code is working properly by setting up some print/debug.logs.

    PickupManager.cs

        public void LoadAndDeactivateInteractedObjects()
        {
            HashSet collectedPickups = SaveData.Instance.collectedPickups;
            
            print("Called");
    
            // Deactivate pickups that have been collected
            foreach (GameObject pickup in GameObject.FindGameObjectsWithTag("Pickup"))
            {
                string pickupID = pickup.name; // Assuming the name of the object is its ID
     
                print(pickupID);
    
                if (collectedPickups.Contains(pickupID))
                {
                    print("found");
                    pickup.SetActive(false); // Deactivate the pickup
                    print("deactivated");
                }
            }
        }
Viewing 15 posts - 1 through 15 (of 46 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: