how do I add a specified number to a string?
so in my game I basically have a shop that I want to use in order to fake/spoof microtranactions. In order to this, I have a button that I would like to use to increment the players currency by a specified value, in this case 50 but at the moment my script is just resetting the value to nothing.
Any ideas on how to do this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class IAP : MonoBehaviour
{
public Text currencyText;
public int currencyToAdd;
private void Start()
{
currencyText.text = "Currency: " + GameManager.Instance.currency.ToString();
currencyToAdd = 0;
}
public void _50Currency()
{
currencyToAdd = 50;
currencyText.text = "Currency: " + GameManager.Instance.currency.ToString() + currencyToAdd;
}
}
Answer by Lilius · Feb 28, 2018 at 06:47 PM
You are not increasing currency, you are concatenating currencyToAdd to the string. Try this instead:
public void _50Currency()
{
currencyToAdd = 50;
GameManager.Instance.currency += currencyToAdd;
currencyText.text = "Currency: " + GameManager.Instance.currency.ToString();
}
}
Agreed, i ll just add that you do not need to convert to string, it is done automatically !
Your answer
Follow this Question
Related Questions
PlayerPrefs not saving after restarting level in editor 1 Answer
PlayerPrefs defaulting to 0 4 Answers
Unity builds with priop playerprefs 0 Answers
Saving an image's state 2 Answers
How to save SetActive states 0 Answers