• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ppapa2000 · Mar 29, 2017 at 02:49 PM · collidersnpcproblem during runtimeshopcoins

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
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Mar 28, 2017 at 06:51 PM 0
Share

At a glance, you may be adding multiple listeners.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Select a prefab from shop to Player - 2D Game 0 Answers

npc collider 1 Answer

physics.OverlapSphere colliders 1 Answer

2D Coins and Points 1 Answer

No gravity on NPC. Floating above the ground. 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges