- Home /
Buying upgrade twice while I shouldn't.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour {
public float movespeed;
public int apples;
public GameObject NewPrefab;
public int coins;
public Text cointext;
public Text appletext;
public int maxApples;
public Text Dialogue;
public Text ButtonTextYes;
public Text ButtonTextNo;
public Button NPCYes;
public Button NPCNo;
public int upgradeCost;
void Start () {
maxApples = 3;
movespeed = 0.1f;
coins = 0;
apples = 0;
upgradeCost = 15;
cointext.text = "Coins: " + coins.ToString();
appletext.text = "Apples: " + apples.ToString();
Dialogue.text = "";
NPCYes.gameObject.SetActive(false);
NPCNo.gameObject.SetActive(false);
ButtonTextYes.text = "";
ButtonTextNo.text = "";
}
void Update () {
transform.Translate(Input.GetAxis("Horizontal") * movespeed, 0f, Input.GetAxis("Vertical") * movespeed);
}
void OnTriggerStay(Collider other) {
if (other.gameObject.CompareTag("PickUp")) {
apples = apples + 1;
appletext.text = "Apples: " + apples.ToString();
if (apples == maxApples) {
Destroy(other.gameObject);
}
}
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.CompareTag("Drop"))
{
coins = coins + apples * 1;
apples = 0;
appletext.text = "Apples: 0";
cointext.text = "Coins: " + coins.ToString();
Instantiate(NewPrefab, new Vector3(-1.77f, 1f, 3.75f), Quaternion.identity);
}
else if (other.gameObject.CompareTag("NPC")) {
Dialogue.text = "Would you like to purchase this upgrade?";
NPCYes.gameObject.SetActive(true);
NPCNo.gameObject.SetActive(true);
ButtonTextYes.text = "Yes";
ButtonTextNo.text = "No";
if (coins >= upgradeCost)
{
NPCYes.onClick.AddListener(OnClickYes1);
}
else {
NPCYes.onClick.AddListener(OnClickYes2);
}
NPCNo.onClick.AddListener(OnClickNo);
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("NPC"))
{
Dialogue.text = "";
NPCYes.gameObject.SetActive(false);
NPCNo.gameObject.SetActive(false);
}
}
void OnClickYes1() {
Debug.Log("l");
coins = coins - upgradeCost;
maxApples = maxApples + 1;
cointext.text = "Coins: " + coins.ToString();
upgradeCost = upgradeCost + 3;
Dialogue.text = "Enjoy your upgrade!";
}
void OnClickYes2() {
Debug.Log("k");
Dialogue.text = "You don't have enough coins";
}
void OnClickNo() {
Dialogue.text = "Perhaps another time then.";
}
}
Whenever I come close to the npc and then buy the upgrade, it buys it once, then the second time twice and so on. Why is this happening?
Comment
Your answer
Follow this Question
Related Questions
2D Coins and Points 1 Answer
No gravity on NPC. Floating above the ground. 2 Answers
physics.OverlapSphere colliders 1 Answer
Need help with my shop system (spawning same weapon no matter what) 1 Answer
npc collider 1 Answer