Forum begins after the advertisement:


[Part 9] Button can not call for function in the Menu

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 9] Button can not call for function in the Menu

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #16305
    pepecry
    Level 6
    Participant
    Helpful?
    Up
    0
    ::

    I was making the Menu and when it got the the part where you made a script fadeUi. Then import that script to the Main Menu and go to the Button to set onclick. It should have the FadeUI.FadeUIOut like in the video
    fade UI in button
    But in my prject. I cannot find that function anywhere at you could see in the picture below
    Button assign in project
    The code is the same as video

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class fadedUI : MonoBehaviour
    {
    
        CanvasGroup CanvasGroup;
    
        private void Awake()
        {
            CanvasGroup = GetComponent<CanvasGroup>();
        }
    
        public void fadeUIOut(float _seconds)
        {
            StartCoroutine(fadeOut(_seconds));
        }
        public void fadeUIIn(float _seconds)
        {
            StartCoroutine(fadeIn(_seconds));
        }
        IEnumerator fadeOut(float _seconds)
        {
            CanvasGroup.interactable = false;
            CanvasGroup.blocksRaycasts = false;
            CanvasGroup.alpha = 1;
            while(CanvasGroup.alpha > 0)
            {
                CanvasGroup.alpha -= Time.unscaledDeltaTime / _seconds;
                yield return null;
            }
            yield return null;
        }
        IEnumerator fadeIn(float _seconds)
        {
            
            CanvasGroup.alpha = 0;
            while (CanvasGroup.alpha <  1)
            {
                CanvasGroup.alpha += Time.unscaledDeltaTime / _seconds;
                yield return null;
            }
            CanvasGroup.interactable = true;
            CanvasGroup.blocksRaycasts = true;
            yield return null;
        }
        // Start is called before the first frame update
        void Start()
        {
            
        }
    
        // Update is called once per frame
        void Update()
        {
            
        }
    }
    

    and I had assign the canvasgroup and the script to all of my menu
    Main Menu Inspector
    Option Menu Inspector
    And I already tried to turn off unity and reopen it but doesn’t work at all. Did I made anything wrong?
    Update, After serveral try of turn on and off. It work now.

    #16306
    pepecry
    Level 6
    Participant
    Helpful?
    Up
    0
    ::

    But I have another question. Why my button like kinda dark. How can I make it look clearly like in the video?
    Button the Main Menu

    #16307
    Chloe Lim
    Level 9
    Moderator
    Helpful?
    Up
    0
    ::

    could you send a screenshot of the inspector of one of the button parents and their text child object?

    #16308
    pepecry
    Level 6
    Participant
    Helpful?
    Up
    0
    ::

    Umm I figure out how to fix that by changing the Vertex Color of the text. Thank you for attend to helping me but I will need you to help me to for another bug. So I just only make the unlock wall jump and now I decide to make the unlock dash and unlock double jump. As the video. I copy and paste and then change the script. The unlocking method look fine but the problem come with the save. before of that. I could saved and load normally. But when I added the unlock doublejump and unlock dash. The save function said it saved but when I turn off and open the game. it said this errors
    End beyond stream
    So I remember that Terrence has upload a post to fix this by delete this the save file. I had tried but still not work. As you could see in the record below
    test saved
    In the player controller well it kinda simple. I just add these 3

    public bool unlockWalljump;
    public bool unlockDash;
    public bool unlockDbJump;

    and
    else if (!isonGround() && dbJumpCounter < maxdbJump && Input.GetButtonDown("Jump") && unlockDbJump)
    for the unlock script. All there of them is just cloning so i will just post one

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class UnlockDbJump : MonoBehaviour
    {
        // Start is called before the first frame update
        [SerializeField] GameObject canvasUI;
        bool used;
        void Start()
        {
            if (Move.Instance.unlockDbJump)
            {
                Destroy(gameObject);
            }
        }
    
        // Update is called once per frame
        private void OnTriggerEnter2D(Collider2D collision)
        {
            if (collision.CompareTag("Player") && !used)
            {
                used = true;
                StartCoroutine(ShowUI());
            }
        }
    
        IEnumerator ShowUI()
        {
            yield return new WaitForSeconds(0.5f);
            canvasUI.SetActive(true);
            Move.Instance.unlockDbJump = true;
            yield return new WaitForSeconds(3.5f);
            SaveData.Instance.savePlayerData();
            canvasUI.SetActive(false);
            Destroy(gameObject);
        }
    }
    

    and the load/savePlayerData

     
     public static SaveData Instance;
    public HashSet<string> sceneNames;
     public string altersceneName;
     public Vector2 alterPos;
     public int playerHealth;
     public int playerHeartShards;
     public float playerMana;
     public Vector2 playerPosition;
     public string lastScene;
     public int playerMaxHealth;
     public bool playerUnlockWallJump;
     public bool playerUnlockDash;
     public bool playerUnlockDbJump;
    public void savePlayerData()
     {
         using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.player.data")))
         {
    
             playerHealth = Move.Instance.Health;
             writer.Write(playerHealth);
    
             playerMana = Move.Instance.Mana;
             writer.Write(playerMana);
    
             playerUnlockWallJump = Move.Instance.unlockWalljump;
             writer.Write(playerUnlockWallJump);
    
             playerPosition = Move.Instance.transform.position;
             writer.Write(playerPosition.x);
             writer.Write(playerPosition.y);
    
             lastScene = SceneManager.GetActiveScene().name;
             writer.Write(lastScene);
    
             playerHeartShards = Move.Instance.heartShards;
             writer.Write(playerHeartShards);
    
             playerMaxHealth = Move.Instance.MaxHealth;
             writer.Write(playerMaxHealth);
    
             playerUnlockDash = Move.Instance.unlockDash;
             writer.Write(playerUnlockDash);
    
             playerUnlockDbJump = Move.Instance.unlockDbJump;
             writer.Write(playerUnlockDbJump);
         }
     }
    
     public void loadPlayerData()
     {
         if (File.Exists(Application.persistentDataPath + "/save.player.data"))
         {
             using (BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.player.data")))
             {
                 playerHealth = reader.ReadInt32();
                 playerMana = reader.ReadSingle();
                 playerUnlockWallJump = reader.ReadBoolean();
                 playerUnlockDash = reader.ReadBoolean();
                 playerUnlockDbJump = reader.ReadBoolean();
                 playerPosition.x = reader.ReadSingle();
                 playerPosition.y = reader.ReadSingle();
    
                 lastScene = reader.ReadString();
                 playerHeartShards = reader.ReadInt32();
                 playerMaxHealth = reader.ReadInt32();
    
                 SceneManager.LoadScene(lastScene);
                 Move.Instance.transform.position = playerPosition;
                 Move.Instance.MaxHealth = playerMaxHealth;
                 Move.Instance.Health = playerHealth;
                 Move.Instance.Mana = playerMana;
                 Move.Instance.unlockWalljump = playerUnlockWallJump;
                 Move.Instance.unlockDash = playerUnlockDash;
                 Move.Instance.unlockDbJump = playerUnlockDbJump;
                 Move.Instance.heartShards = playerHeartShards;
                 Debug.Log("Loaded position: " + playerPosition);
                 Debug.Log("Loaded health: " + playerHealth);
                 Debug.Log("Loaded mana: " + playerMana);
                 Debug.Log("Loaded unlock wall jump: " + playerUnlockWallJump);
                 Debug.Log("Loaded heart shards: " + playerHeartShards);
                 Debug.Log("Loaded Health: " + playerHealth);
                 Debug.Log("Loaded unlock Dash: " + playerUnlockDash);
                 Debug.Log("Loaded unlock dbjump: " + playerUnlockDbJump);
                 Debug.Log("Loaded Max Health: " + playerMaxHealth);
             }
             Debug.Log("Player data loaded successfully");
         }
         else
         {
             Debug.Log("File doesn't exist");
             Move.Instance.Health = Move.Instance.MaxHealth;
             Move.Instance.heartShards = 0;
             Move.Instance.Mana = 0.5f;
             Move.Instance.unlockWalljump = false;
             Move.Instance.unlockDash = false;
             Move.Instance.unlockDbJump = false;
         }
     }
    #16310
    Chloe Lim
    Level 9
    Moderator
    Helpful?
    Up
    0
    ::

    ok firstly, check that the scenes are added to the build,

    Then we need to check if everything is being saved in SavePlayerData() properly, put a debug log before and after saving the lastScenevariable through scenemanager. Do the same with the load method by adding a print message before retrieving lastScene

    After that, try a second test by commenting out all the last scene variables to see if there is a difference

    #16313
    pepecry
    Level 6
    Participant
    Helpful?
    Up
    0
    ::

    Ok so as your recommended. I has check the build and yes, all the scenes are added to the build
    Build Manager
    And I added the code as you said like this

     public void savePlayerData()
     {
         using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.player.data")))
         {
    
             playerHealth = Move.Instance.Health;
             writer.Write(playerHealth);
    
             playerMana = Move.Instance.Mana;
             writer.Write(playerMana);
    
             playerUnlockWallJump = Move.Instance.unlockWalljump;
             writer.Write(playerUnlockWallJump);
    
             playerPosition = Move.Instance.transform.position;
             writer.Write(playerPosition.x);
             writer.Write(playerPosition.y);
             playerHeartShards = Move.Instance.heartShards;
             writer.Write(playerHeartShards);
    
             playerMaxHealth = Move.Instance.MaxHealth;
             writer.Write(playerMaxHealth);
    
             playerUnlockDash = Move.Instance.unlockDash;
             writer.Write(playerUnlockDash);
    
             playerUnlockDbJump = Move.Instance.unlockDbJump;
             writer.Write(playerUnlockDbJump);
             Debug.Log("Save get before last scene");
             lastScene = SceneManager.GetActiveScene().name;
             writer.Write(lastScene);
             Debug.Log("Save get after last scene");
         }
     }
    
     public void loadPlayerData()
     {
         if (File.Exists(Application.persistentDataPath + "/save.player.data"))
         {
             using (BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.player.data")))
             {
                 playerHealth = reader.ReadInt32();
                 playerMana = reader.ReadSingle();
                 playerUnlockWallJump = reader.ReadBoolean();
                 playerUnlockDash = reader.ReadBoolean();
                 playerUnlockDbJump = reader.ReadBoolean();
                 playerPosition.x = reader.ReadSingle();
                 playerPosition.y = reader.ReadSingle();
                 playerHeartShards = reader.ReadInt32();
                 playerMaxHealth = reader.ReadInt32();
                 lastScene = reader.ReadString();
                 Move.Instance.transform.position = playerPosition;
                 Move.Instance.MaxHealth = playerMaxHealth;
                 Move.Instance.Health = playerHealth;
                 Move.Instance.Mana = playerMana;
                 Move.Instance.unlockWalljump = playerUnlockWallJump;
                 Move.Instance.unlockDash = playerUnlockDash;
                 Move.Instance.unlockDbJump = playerUnlockDbJump;
                 Move.Instance.heartShards = playerHeartShards;
                 Debug.Log("Loaded position: " + playerPosition);
                 Debug.Log("Loaded health: " + playerHealth);
                 Debug.Log("Loaded mana: " + playerMana);
                 Debug.Log("Loaded unlock wall jump: " + playerUnlockWallJump);
                 Debug.Log("Loaded heart shards: " + playerHeartShards);
                 Debug.Log("Loaded Health: " + playerHealth);
                 Debug.Log("Loaded unlock Dash: " + playerUnlockDash);
                 Debug.Log("Loaded unlock dbjump: " + playerUnlockDbJump);
                 Debug.Log("Loaded Max Health: " + playerMaxHealth);
                 Debug.Log("Save Load before lastscene");
                 SceneManager.LoadScene(lastScene);
                 Debug.Log("Save Load after lastscene");
             }
             Debug.Log("Player data loaded successfully");
         }
         else
         {
             Debug.Log("File doesn't exist");
             Move.Instance.Health = Move.Instance.MaxHealth;
             Move.Instance.heartShards = 0;
             Move.Instance.Mana = 0.5f;
             Move.Instance.unlockWalljump = false;
             Move.Instance.unlockDash = false;
             Move.Instance.unlockDbJump = false;
         }
     }

    Is it correct? if it correct so the result that it print out 3 times save get before and after last scene like this
    Save get
    save game
    After that, when I start the game, the end of stream disapear, Left me with another bug that when I save, I only have 4 hp. But when the game load. I got max potential Hp I could get is 10.
    hp max after save
    and what console print out is very weird
    Console after save
    As you could see as the MaxHp and the heartShards. The number is very weird.
    Why it could be like this when I just add some debuglog and change the position of it in the code block. Sorry for making you get into this trouble and thank you so much for helping me from start to now

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: