I’m trying to create a button that spends money on a game I’m working on (this is my first time trying to program so I apologize for any huge errors). I have it so that it shows how much money I have when it starts but when I buy it isn’t changing to the new value. Any ideas?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class first : MonoBehaviour
{
public int money;
public Text countMoney;
void Start()
{
money=1000000;
SetMoneyText ();
}
void update()
{
SetMoneyText ();
}
public void UpgrademilitaryFunc () {
money = money-50000;
}
void SetMoneyText ()
{
countMoney.text = "money: " + money.ToString ();
}
}
I would also suggest you not update your text in Update. Just update it when your player buys something, not every frame if it’s not necessary to do so.
Per your example, why not call the method in UpgrademilitaryFunc after you update the money.
Your code is not very optimized. You should not call SetMoneyText in Update, it executes this every frame.
Also, why are you initializing money in Start ? instead, initialize it directly during declaration, so if you want to adjust the starting in Inpesctor, it will take your value setup.
Instead your script could implement IPointerClickHandler interface.
Using GameRules extension, Here is the code I could generate in less than a minute:
using UnityEngine.AI;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace GameRules
{
public class Money_Tanks_RuleObjectFlow : MonoBehaviour, IPointerClickHandler
{
public int money = 1000000;
public Text countMoney;
// Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.
void Start ()
{
// Logic State SetMoneyText
perform_SetMoneyText();
}
public void OnPointerClick (PointerEventData eventData)
{
// Logic State DecreaseMoney
perform_DecreaseMoney(50000);
// Logic State SetMoneyText
perform_SetMoneyText();
}
void perform_DecreaseMoney (int moneySpent)
{
money = money - moneySpent;
}
void perform_SetMoneyText ()
{
countMoney.text = "money: " + money.ToString ();
}
}
}
While I can agree his code isn’t the best designed code, if he has a button, he’s fine using the onclick method to tie it in with the inspector.
I’m also not certain if you’re just adding the extra stuff to try and advertise your asset or not, which probably would be a bit of an overkill for someone starting out. lol.
I would say it is both to advertise a bit, and also to help people as I provide the generated code. Actually after having developed two mobile games in the past, I get really upset by always retypying same things. So I created this extension (it took me two years) to speed and make code more maintainable, even if you still need some knowledge with Unity C# API. This extension reuses similar concepts from a software used in banking and insurance sectors that I am used to.
Also - why implement the interface yourself when Button already does? Let me attach this thing that takes click input to a button that…also takes click input?
Ultimately - off topic. The correct answer given the initial question is to capitalize the U. Given OP’s newness to Unity, that should be the end of it