Search Unity

Button sprite goes back to previous one after changing and restarting the game

Discussion in 'Scripting' started by moneysolutionsxyz, Sep 19, 2022.

  1. moneysolutionsxyz

    moneysolutionsxyz

    Joined:
    Jul 17, 2022
    Posts:
    14
    Hello guys,
    I am working on a game which have multiple levels. when i win the the i want to change the Level Button sprite and its working until restarting the entire game its goes back to previous sprite. I didn't want the sprite go back to its previous sprite. Please Help Me!
    Complete Level Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class CompleteLevel : MonoBehaviour
    8. {
    9.     public GameObject Star1Panel, Star2Panel, Star3Panel;
    10.     public SceneFader sceneFader;
    11.     public string menuSceneName = "MainMenu";
    12.     public Animator animator;
    13.     public static bool H3 = false, H2 = false, H1 = false;
    14.  
    15.     private void Start()
    16.     {
    17.         Star1Panel.SetActive(false);
    18.         Star2Panel.SetActive(false);
    19.         Star3Panel.SetActive(false);
    20.     }
    21.     private void Update()
    22.     {
    23.         CheckLives();
    24.     }
    25.  
    26.     public void CheckLives()
    27.     {
    28.         if(PlayerStats.health == 3)
    29.         {
    30.             animator.Play("3Star");
    31.             Star3Panel.SetActive(true);
    32.             H3 = true;
    33.         }
    34.         if (PlayerStats.health == 2)
    35.         {
    36.             animator.Play("2Star");
    37.             Star2Panel.SetActive(true);
    38.             H2 = true;
    39.         }
    40.         if (PlayerStats.health == 1)
    41.         {
    42.             animator.Play("1Star");
    43.             Star1Panel.SetActive(true);
    44.             H1 = true;
    45.         }
    46.     }
    47.  
    48.     public void MainMenu()
    49.     {
    50.         sceneFader.FadeTo(menuSceneName);
    51.     }
    52.     public void Restart()
    53.     {
    54.         Time.timeScale = 1f;
    55.         sceneFader.FadeTo(SceneManager.GetActiveScene().name);
    56.     }
    57. }
    58.  
    Game Manager Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameManager : MonoBehaviour
    6. {
    7.     public static bool gameIsOver;
    8.  
    9.     public GameObject gameOverUI;
    10.     public GameObject completeLevelUI;
    11.     //public string nextLevel = "Level02";
    12.     public int levelToUnlock = 2;
    13.     public int level = 1;
    14.  
    15.     private void Start()
    16.     {
    17.         gameIsOver = false;
    18.         Time.timeScale = 1f;
    19.     }
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if (gameIsOver)
    24.         {
    25.             return;
    26.         }
    27.  
    28.         if(PlayerStats.health <= 0)
    29.         {
    30.             EndGame();
    31.         }
    32.     }
    33.     public void EndGame()
    34.     {
    35.         Time.timeScale = 0f;
    36.         gameOverUI.SetActive(true);
    37.         gameIsOver = true;
    38.        
    39.     }
    40.     public void WinLevel()
    41.     {
    42.         gameIsOver = true;
    43.         completeLevelUI.SetActive(true);
    44.         PlayerPrefs.SetInt("levelReached", levelToUnlock);
    45.         PlayerPrefs.SetInt("levelComplete", level);
    46.     }
    47. }
    48.  
    Level Selector Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class LevelSelector : MonoBehaviour
    7. {
    8.     public SceneFader fader;
    9.     public Button[] levelButtons;
    10.     public Sprite lockedBtnImg, currentBtnImg, star3Img, star2Img, star1Img;
    11.  
    12.  
    13.     void Start()
    14.     {
    15.         int levelReached = PlayerPrefs.GetInt("levelReached", 1);
    16.         int levelComplete = PlayerPrefs.GetInt("levelComplete", 1);
    17.         for (int i = 0; i < levelButtons.Length; i++)
    18.         {
    19.             if(i + 1 > levelReached)
    20.             {
    21.                 levelButtons[i].interactable = false;
    22.                 levelButtons[i].GetComponent<Image>().sprite = lockedBtnImg;
    23.             }
    24.             else if(i + 1 == levelReached)
    25.             {
    26.                 levelButtons[i].interactable = true;
    27.                 levelButtons[i].GetComponent<Image>().sprite = currentBtnImg;
    28.             }
    29.             if (CompleteLevel.H3 == true)
    30.             {
    31.                 for (int x = 0; x < levelButtons.Length; x++)
    32.                 {
    33.                     if (x + 1 == levelComplete)
    34.                     {
    35.                         levelButtons[x].GetComponent<Image>().sprite = star3Img; // After restarting the game in unity this image rollback to previous one
    36.                         PlayerPrefs.Save();
    37.                     }
    38.                 }
    39.             }
    40.             if (CompleteLevel.H2 == true)
    41.             {
    42.                 for (int y = 0; y < levelButtons.Length; y++)
    43.                 {
    44.                     if (y + 1 == levelComplete)
    45.                     {
    46.                         levelButtons[y].GetComponent<Image>().sprite = star2Img;// After restarting the game in unity this image rollback to previous one
    47.                         PlayerPrefs.Save();
    48.                     }
    49.                 }
    50.             }
    51.             if (CompleteLevel.H1 == true)
    52.             {
    53.                 for (int z = 0; z < levelButtons.Length; z++)
    54.                 {
    55.                     if (z + 1 == levelComplete)
    56.                     {
    57.                         levelButtons[z].GetComponent<Image>().sprite = star1Img;// After restarting the game in unity this image rollback to previous one
    58.                         PlayerPrefs.Save();
    59.                     }
    60.                 }
    61.             }
    62.         }
    63.     }
    64.     public void Select(string levelName)
    65.     {
    66.         fader.FadeTo(levelName);
    67.     }
    68. }
    69.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Steps to success will be to:

    - understand the mechanism that changes it

    - understand the mechanism that bypasses this

    - fix that.

    Perhaps you aren't even running the correct "set the sprite to what I want" code?

    Here's how to begin debugging:

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    When in doubt, print it out!(tm)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  3. moneysolutionsxyz

    moneysolutionsxyz

    Joined:
    Jul 17, 2022
    Posts:
    14
    anyone else? Please help me :(
     
  4. TheDevloper

    TheDevloper

    Joined:
    Apr 30, 2019
    Posts:
    69
    you can use singletons on the game manager