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
- This topic has 5 replies, 2 voices, and was last updated 1 month, 1 week ago by pepecry.
-
AuthorPosts
-
November 12, 2024 at 5:01 pm #16305::
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
The code is the same as video<code>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() { } } </code>
and I had assign the canvasgroup and the script to all of my menu
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.November 12, 2024 at 5:11 pm #16306::But I have another question. Why my button like kinda dark. How can I make it look clearly like in the video?
November 12, 2024 at 6:27 pm #16307::could you send a screenshot of the inspector of one of the button parents and their text child object?
November 12, 2024 at 7:21 pm #16308::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
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<code>public bool unlockWalljump; public bool unlockDash; public bool unlockDbJump;</code>
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<code>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); } } </code>
and the load/savePlayerData
<code> 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; } }</code>
November 13, 2024 at 12:23 am #16310::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 thelastScene
variable through scenemanager. Do the same with the load method by adding a print message before retrieving lastSceneAfter that, try a second test by commenting out all the last scene variables to see if there is a difference
November 13, 2024 at 4:41 am #16313::Ok so as your recommended. I has check the build and yes, all the scenes are added to the build
And I added the code as you said like this<code> 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; } }</code>
Is it correct? if it correct so the result that it print out 3 times save get before and after last scene like this
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. and what console print out is very weird 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 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: