0

I'm trying to make a simple condition:

If the value I have, is less than the price it costs the item, the button is disabled.

If the value I have, is greater than or equal to the price it costs the item the button is enabled and I can buy it.

But when I test, I have some problems.

First, if I have less than the item cost the button is enabled, and only when I click on it it is when it disables.

Second, if I have less than the item cost and I click on it it disables, but if I get enough to purchase the item, the button is not enabled again.

How do I to be checked these variables all the time? If I have enough the button is enabled if you do not have it disables.

Bellow my scrip:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class BuySkin : MonoBehaviour {

    public int price;
    public Button buyBee1;

    void OnEnable ()
    {
        //Register Button Events
        buyBee1.onClick.AddListener (() => buySkin (buyBee1));
    
    }


    public void buySkin(Button button)
    { 
        if (BeeCoinScore.coin >= price) {
            BeeCoinScore.coin -= price;
            buyBee1.interactable = false;

        }

        if (BeeCoinScore.coin < price) {
            buyBee1.interactable = false;
        }
    }

    void OnDisable ()
    {
        //Un-Register Button Events
        buyBee1.onClick.RemoveAllListeners ();

    }
}
0

2 Answers 2

1

Try this out with some prefabs!

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class GameController : MonoBehaviour
{

    public int coins;
    private int spherePrice = 100, cubePrice = 50;

    public GameObject player;
    public GameObject[] availablePrefabs;
    public List<GameObject> mySkins;

    public Button btnSphere, btnCube;
    public Text txtSphere, txtCube;

    void Start ()
    {
        string serializedMySkins = PlayerPrefs.GetString ("skins", "");
        string serializedPlayer = PlayerPrefs.GetString ("player", "");

        // skins desserialization
        if (serializedMySkins == "")
            mySkins = new List<GameObject> ();
        else {
            var a = serializedMySkins.Split (',');
            for (int i = 0; i < a.Length; i++) {
                if (a [i] == "Sphere") {
                    mySkins.Add (availablePrefabs [0]);
                } 

                if (a [i] == "Cube") {
                    mySkins.Add (availablePrefabs [1]);
                } 
            }
        }

        // player desserialization
        if (serializedPlayer != "") {
            if (serializedPlayer == "Sphere") {
                player = availablePrefabs [0];
            } 

            if (serializedPlayer == "Cube") {
                player = availablePrefabs [1];
            } 
        } else {
            player = mySkins [0];
        }

        coins = PlayerPrefs.GetInt ("coins", 0);
        coins = 1000;
    }

    void Update ()
    {
        if (mySkins.Contains (availablePrefabs [0])) { 
            txtSphere.text = "Usar esfera";
        } else {
            btnSphere.interactable = coins >= spherePrice;
        }

        if (mySkins.Contains (availablePrefabs [1])) {
            txtCube.text = "Usar cubo";
        } else {
            btnCube.interactable = coins >= cubePrice;
        }
    }

    public void play ()
    {
        player = (GameObject)Instantiate (player, new Vector2 (0, 0), Quaternion.identity);
    }

    public void verifySkin (GameObject skinPrefab)
    {
        if (mySkins.Contains (skinPrefab)) {
            useSkin (skinPrefab);
        } else if (coins >= priceOf (skinPrefab)) {
            buySkin (skinPrefab, priceOf (skinPrefab));
        }
    }

    public void buySkin (GameObject skinPrefab, int price)
    {
        mySkins.Add (skinPrefab);
        coins -= price;

        string skinsHash = "";
        for (int i = 0; i < mySkins.Count; i++) {
            skinsHash += mySkins [i].name + ",";
        }

        Debug.Log (skinsHash);

        PlayerPrefs.SetInt ("coins", coins);
        PlayerPrefs.SetString ("skins", skinsHash);

        PlayerPrefs.Save ();
    }

    public void useSkin (GameObject skinPrefab)
    {
        player = skinPrefab;
        PlayerPrefs.SetString ("player", player.name);
        PlayerPrefs.Save ();
    }

    private int priceOf (GameObject skinPrefab)
    {
        if (skinPrefab == availablePrefabs [0])
            return spherePrice;
        else if (skinPrefab == availablePrefabs [1])
            return cubePrice;
        else
            return 0;
    }
}
Sign up to request clarification or add additional context in comments.

11 Comments

i think the above script is for some sort of buy window, so you probably dont need to update that every frame.
It's not working, when I do not possess enough resource to purchase the item, the button is enabled. If you do not have resources it should be disabled until I have enough resource to purchase the item.
@AlanVieiraRezende have you tried just putting it in OnEnable?
@yes I am trying since you answered my question, but without success yet.
On the code above, you see that in both conditions you're setting buyBee1.interactable as false right? Shouldn't one of them be true?
|
0

OnEnable()is called when the object becomes enabled and active. you need Update() as it is getting called every frame it will check whether your value is less than or greater than price of item.You may also try like this.

// I think that you are making an buymenu, so you can disable and enable your menu with ui button and check money you have


     using System.Collections;
        using UnityEngine.UI;

        public class BuySkin : MonoBehaviour 
    {

            public int price;
            public static int money;// money you have
            public Button thisbuyBee1;
    public void buychkr()
    {
    if(price>= money)
    {
    thisbuyBee1.interactable = false;
    }
    else
    {
    thisbuyBee1.interactable = true;
    }
    }
    void Update()
    {
    buychkr();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.