How do i make UI buttons work like a buy button of a Store?
I have already created this script:
using UnityEngine; using System.Collections;
public class add : MonoBehaviour {
// Use this for initialization
public void Start () {
//PlayerPrefs.DeleteAll ();
Store.instance.CurrentAnimator = 3;
int firstRun = PlayerPrefs.GetInt("savedFirstRun", 0);
if (Coin.coinsCollected >= 4 && firstRun == 0) {
PlayerPrefs.SetInt("savedFirstRun", 1);
Coin.coinsCollected -= 4;
}
else {
Debug.Log ("Not enough coins or already bought character");
}
}
The problem comes in when the buy button gets pressed and Coin.coinsCollected < 4, PlayerPrefs.SetInt("savedFirstRun", 1); gets called and firstRun == 1, dont know why Playerp=Prefs.SetInt its bypassing the if statement. Because of this im not able to take the coins from the player when Coin.coinsCollected > 4 because firstRun ==1 and it calls the else statement. The method only works when the first time the button gets pressed it also buy the character and it will take the coin and if you press the button again it will call the else statement and will not take coins again because firstRun ==1. Basically what i want to do is, if i press the button and i don't have enough coins i want firstRun to still ==0, only when i subtract the coins i want it to change to ==1 so its no longer able to substract the coins. I've been to trying to figure this out all day long hope someone can help.
Answer by yozelectric · Jun 01, 2016 at 05:02 AM
how did you solved it? your question is a help to other developers
Answer by IHackedDeath · Aug 31, 2015 at 02:14 PM
Hi Geft98,
I Have gone through the question and believe that the problem is because you are doing all this in the start function, so it only looks through all the code once at the start of the game.
If you move the if statement to the update function then I think you will find it will begin to work.
I am not 100% on what you are asking as I am not sure where this script is attached in the game and how many gameobjects it is on, or even what gameobject it is attached.
If you would like to provide further information then I am happy to assist.
Kind Regards,
IHackedDeath.
Answer by Geft98 · Sep 03, 2015 at 02:09 AM
Never mind, figured it out, thanks for trying to help IHackedDeath!!
Your answer
Follow this Question
Related Questions
IAP dont work on android device (unity 2019.1.4// IAP 1.22) 0 Answers
How to save bought items and show them on my inventory ? 0 Answers
Trying to make the Cursor Invisible when a UI button is pressed 2 Answers
How to add random number on a ui button?? 0 Answers
Change a Button image for 1 second and than switch back to the old one 0 Answers