P
P
PopTop2021-09-05 16:01:54
Unity
PopTop, 2021-09-05 16:01:54

How to save in OnClick?

Hello, I apologize for asking you again for help with my stupid question, I honestly tried to do it for a long time and googled it. In general, the plan was to make as many OnClick methods in the SpriteSwapper script as there are shopping buttons, for each method to make its own key in PlayerPrefs Button1, Button2, etc. And through OnClick1 in the properties of the button, initialize each individual method. At the moment I'm trying to make it so that when I click on the button, I change the color (everything works here) and save the color for this button, but I change the color when I click, then I restart and for some reason disabledSprite is applied to all buttons and initialization in Inspector Button flies, it is when you restart the game (screen in the album). The plans are to apply the purchase method to each button, with the condition of triggering only 1 time, but I have not yet reached that point.

Also, I am struggling with solving the problem that when my hero spawns through the skin change script, the death window from the UI, which is hidden, is not initialized on it. When everything worked for me without a hero change script, I simply initialized the public GameObject Lose; and in the right place called Lose.SetActive(true); I read that you should make Lose private and call it Lose = GameObject.Find("EndGameLose").GetComponent(); Then I read that it's not easy to call a GameObject if it's hidden, so I created an empty object in the UI and nested EndGameLose in it and tried to call pustushka.transform.GetChild(0).gameObject.SetActive(true); which also failed.
Screenshots are all in the album https://yapx.ru/v/OC8Iz

public class SpriteSwapper : MonoBehaviour
    {
       public class SpriteSwapper : MonoBehaviour
    {
        public Sprite enabledSprite;
        public Sprite disabledSprite;
        private bool m_swapped = true;
        private bool m_swapped1 = true;
        private bool m_swapped2 = true;
        private Image m_image;
    
        public Button button;

        public void Awake()
        {
            m_image = GetComponent<Image>();
           
        }

        private void Start()
        {
            if (PlayerPrefs.GetInt("Button1") == 1)
            {
                button = this.gameObject.GetComponent<Button>();
                m_image.sprite = disabledSprite;
                //   Debug.Log(PlayerPrefs.GetInt("Button1"));
           
            }

            if (PlayerPrefs.GetInt("Button2") == 1)
            {
                button = this.gameObject.GetComponent<Button>();
                m_image.sprite = disabledSprite;
               // Debug.Log(PlayerPrefs.GetInt("Button1"));
            }
        }

        public void SwapSprite()
        {
            if (m_swapped)
            {
                m_swapped = false;
                m_image.sprite = disabledSprite;

            }
        }

        public void OnClick1()
        {
            if (m_swapped1)
            {
                m_swapped1 = false;
                m_image.sprite = disabledSprite;
                PlayerPrefs.SetInt("Button1", 1);
                Debug.Log(PlayerPrefs.GetInt("Button1"));
            }
        }

        public void OnClick2()
        {
            if (m_swapped2)
            {
                m_swapped2 = false;
                m_image.sprite = disabledSprite;
                PlayerPrefs.SetInt("Button2", 1);
               Debug.Log(PlayerPrefs.GetInt("Button2", 1));
            }
        }
    }


public class ColorSwapper : MonoBehaviour
    {
        public Color enabledColor;
        public Color disabledColor;
        private bool m_swapped = true;
        private bool m_swapped1 = true;
        private Image m_image;
        private Image m_image1;

        private void Awake()
        {
            m_image = GetComponent<Image>();
            m_image1 = GetComponent<Image>();
        }

        public void SwapColor()
        {
            if (m_swapped)
            {
                m_swapped = false;
                m_image.color = disabledColor;
            }
            else
            {
                m_swapped = true;
                m_image.color = enabledColor;
            }
        }
        public void SwapColor1()
        {
            if (m_swapped1)
            {
                m_swapped1 = false;
                m_image1.color = disabledColor;
            }
            else
            {
                m_swapped1 = true;
                m_image1.color = enabledColor;
            }
        }

    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
PopTop, 2021-09-06
@PopTop

I made buttons on which I hung the Buy script, if you click on it and if there is enough money, then this button disappears, only there is a small problem, if this window is open in advance, then everything works fine for me, when you click on the button, it immediately disappears and when When you re-enter the game, everything is saved, but if this shop window is hidden and called to be opened, then when you click on it, the buttons do not disappear, if you re-enter the game, they are no longer on the stage, they are saved by SetActive (false). I tried to stick the start method in OnEnable() and Awake() did not help. What can be wrong? The script hangs on the camera

public class Buy : MonoBehaviour
{
    public GameObject BuyButton1;
    public GameObject BuyButton2;
    int BuySkin1;
    int BuySkin2;


    private void OnEnable()
    {
        BuySkin1 = PlayerPrefs.GetInt("BuySkin1", 1);
        BuySkin2 = PlayerPrefs.GetInt("BuySkin2", 1);
    }

    void Update()
    {
        if (BuySkin1 == 1)
        {
            BuyButton1.SetActive(true);
        }
        else
        {
            BuyButton1.SetActive(false);
        }

        if (BuySkin2 == 1)
        {
            BuyButton2.SetActive(true);
        }
        else
        {
            BuyButton2.SetActive(false);
        }
    }

    public void BuySkins1()
    {
        if (CoinText.Coin >= 5)
        {
            CoinText.Coin -= 5;
            PlayerPrefs.SetInt("Coins", CoinText.Coin);
            BuySkin1 = 2;
            PlayerPrefs.SetInt("BuySkin1", BuySkin1);
        }
    }

    public void BuySkins2()
    {
        if (CoinText.Coin >= 10)
        {
            CoinText.Coin -= 10;
            PlayerPrefs.SetInt("Coins", CoinText.Coin);
            BuySkin2 = 2;
            PlayerPrefs.SetInt("BuySkin2", BuySkin2);
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question