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<GameObject, float> 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,