- Home /
shop script not working
Hi my shop script isn't working I don't know why. If I click buy button the cost of the gameobject doesn't get deducted. I wrote a Debug.Log and the code is working but it is not changing please help.
shop script: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using UnityEngine.EventSystems;
public class shop : MonoBehaviour { public TMPro.TextMeshProUGUI scoreText;
public GameObject Item1;
public GameObject Item2;
public GameObject Item3;
public GameObject Item4;
private Dictionary<GameObject, float> ItemPrices;
void Start ()
{
scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
ItemPrices = new Dictionary<GameObject, float>()
{
{ Item1, 100f },
{ Item2, 2500f },
{Item3, 3500f},
{ Item4, 5000f },
};
}
public void PurchaseItem(GameObject Item)
{ foreach(KeyValuePair item in ItemPrices) { if (item.Key == Item) { // Take away the cost of the item from the player's currency float val = PlayerPrefs.GetFloat ("Highscore"); val -= item.Value; //scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString(); Debug.Log("working"); }
} }
}
and this is the playerhealth script in the die() method in it. When the player dies it saves the score in playerprefs:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using System;
public class PlayerHealth : MonoBehaviour {
public GameObject death;
public int maxHealth = 100;
public static int currentHealth;
public HealthBar healthBar;
public GameObject gameOverPanel;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}
// Update is called once per frame
void Update()
{
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
if( currentHealth <= 0)
{
Die();
}
}
void Die ()
{
Destroy(gameObject);
audioman.PlaySound ("explosion");
Instantiate(death, transform.position, Quaternion.identity);
PlayerPrefs.SetFloat ("Highscore", PlayerPrefs.GetFloat("Highscore", 0) + ScoreScript.scoreValue);
gameOverPanel.SetActive(true);
ScoreScript.scoreValue = 0;
//script.Score.text = (ScoreScript.scoreValue).ToString();
//Time.timeScale = 0f;
}
} Please help. Thanks,
Answer by socialBacons · Jan 17, 2021 at 01:33 PM
I think the problem is here: float val = PlayerPrefs.GetFloat ("Highscore"); val -= item.Value; scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
You are getting the PlaterPref Highscore and assigning it to the float val. Then you change val but don't assign it back to the Highscore. I think you should be doing it like this:
float val = PlayerPrefs.GetFloat ("Highscore"); val -= item.Value; PlayerPrefs.SetFloat("Highscore", val) scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
@socialBacons Thankyou so much man but there is two more problems now 1: if player don't have enough money and clicks the button the points becomes a negative value for eg: if I have 500 points and I click to buy a product of 1000 my points will change to -500 I dont want that. I want it to show a error message "not enough points" I had already mad a Textmeshpro UI text and referenced it to my script but do I need to SetActive() the text. 2: I want to change the buybutton to equip button of the particular item the player bought successfully. Thanks.
You can check first, if the player's currency exceeds the required amount, before purchasing the item. For example:
int price = item.Value;
if (playerCurrency >= price)
{
PurchaseItem();
}
else
{
Debug.Log("not enough money");
}
And how can I change the buy button to equip button of the particular item that player bought successfully and save it in playerpref?
Your answer
Follow this Question
Related Questions
PlayerPrefs, it doesn't change the text after restarting the game 0 Answers
My shop system script is not working 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to load a scene with playerprefs from a ui element 1 Answer