Search Unity

Clicker Games Upgrade button doesn't work properly

Discussion in 'Scripting' started by Siyar, Nov 4, 2018.

  1. Siyar

    Siyar

    Joined:
    Nov 4, 2018
    Posts:
    1
    First of all, I'm new to programming. So I'm making Clicker game where pressing buttons to add money, and then there is upgrade button which "multiplies" the income, but button needs to press twice to work :/ and when changing scene to the shopping scene and coming back from shopping scene the upgrade button resets. I don't know how to fix this and I need help. I was trying to fix this at least 11 hours.

    Click Script:_______________
    using System.Collections;
    using UnityEngine;

    public class Click : MonoBehaviour{
    public UnityEngine.UI.Text moneyText;
    public float moneyAmount = 0;
    public int moneyAmountPerclick = 1;


    public void Clicked()
    {
    GameControlScript1.moneyAmount += moneyAmountPerclick;
    moneyAmount= GameControlScript1.moneyAmount;
    }
    }




    Upgrade Script:___________
    using System.Collections;
    using UnityEngine;

    public class Upgrade : MonoBehaviour
    {

    public Click click;
    public UnityEngine.UI.Text itemInfo;
    public float cost;
    public int count = 0;
    public int clickPower;
    public string ItemName;

    void Update()
    {
    itemInfo.text = ItemName + "\nCost: " + cost + "\nPower: " + clickPower;
    }

    public void PurchasedUpgrade()
    {
    if (click.moneyAmount >= cost)
    {
    click.moneyAmount -= cost;
    count += 1;
    click.moneyAmountPerclick += clickPower;
    cost = Mathf.Round(cost * 1.15f);
    }
    }
    }





    ShopScene Script:_________
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;

    public class ShopControlScript : MonoBehaviour {

    int moneyAmount;
    int isRifleSold;
    int isBallSold;

    public Text moneyAmountText;
    public Text riflePrice;
    public Text ballPrice;

    public Button buyRifleButton;
    public Button buyBallButton;

    // Use this for initialization
    void Start () {
    moneyAmount = PlayerPrefs.GetInt ("MoneyAmount");
    }

    // Update is called once per frame
    void Update () {

    moneyAmountText.text = "Money: " + moneyAmount.ToString() + "$";

    isBallSold = PlayerPrefs.GetInt("IsBallSold");
    isRifleSold = PlayerPrefs.GetInt ("IsRifleSold");

    if (moneyAmount >= 5 && isBallSold == 0)
    buyBallButton.interactable = true;
    else
    buyBallButton.interactable = false;

    if (moneyAmount >= 5 && isRifleSold == 0)
    buyRifleButton.interactable = true;
    else
    buyRifleButton.interactable = false;
    }

    public void buyRifle()
    {
    moneyAmount -= 5;
    PlayerPrefs.SetInt ("IsRifleSold", 1);
    riflePrice.text = "Sold!";
    buyRifleButton.gameObject.SetActive (false);
    }

    public void buyBall()
    {
    moneyAmount -= 5;
    PlayerPrefs.SetInt("IsBallSold", 1);
    ballPrice.text = "Sold!";
    buyBallButton.gameObject.SetActive(false);
    }

    public void exitShop()
    {
    PlayerPrefs.SetInt ("MoneyAmount", moneyAmount);
    SceneManager.LoadScene ("GameScene");
    }

    public void resetPlayerPrefs()
    {
    moneyAmount = 0;
    buyRifleButton.gameObject.SetActive (true);
    buyBallButton.gameObject.SetActive(true);
    riflePrice.text = "Price: 5$";
    ballPrice.text = "Price: 5$";
    PlayerPrefs.DeleteAll ();
    }

    }




    GameScene Script:_______________

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

    public class GameControlScript1 : MonoBehaviour
    {

    public Text moneyText;
    public static int moneyAmount;
    int isBallSold;
    int isRifleSold;
    public GameObject rifle;
    public GameObject ball;

    // Use this for initialization
    void Start()
    {
    moneyAmount = PlayerPrefs.GetInt("MoneyAmount");
    isRifleSold = PlayerPrefs.GetInt("IsRifleSold");
    isBallSold = PlayerPrefs.GetInt("IsBallSold");

    if (isRifleSold == 1)
    rifle.SetActive(true);
    else
    rifle.SetActive(false);

    if (isBallSold == 1)
    ball.SetActive(true);
    else
    ball.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {
    moneyText.text = "Money: " + moneyAmount.ToString() + "$";
    }

    public void gotoShop()
    {
    PlayerPrefs.SetInt("MoneyAmount", moneyAmount);
    SceneManager.LoadScene("ShopScene");
    }
    }
     
  2. Deleted User

    Deleted User

    Guest