Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
This question was closed Oct 04, 2017 at 12:18 AM by Agent27765 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Agent27765 · Sep 27, 2017 at 12:31 AM · script.coin

How to stop execution of a C# script?

Hello I asked the same question but it doesnt seem to be getting any answers so I am asking again. I am trying to make a shop in which if the player doesnt have enough money I want it to stop the adding of the item into the inventory. Unfortunately I dont know how to do that right now.

Shop Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 using UnityEngine;
 
 public class Shop : ActionItem
 {
     public GameObject ManaPotion;
     public GameObject CoinSystem;
 
     private bool subtractWorked = false;
 
     public AudioSource BuyPotion;
     public AudioClip BuyPotionClip;
 
     private void Start()
     {
         BuyPotion.clip = BuyPotionClip;
     }
 
     public void BuyManaPot()
     {
         CoinSystem.GetComponent<CoinSystem>().SubtractCoins(10);
         {
             inventory.AddItem(ManaPotion);
             BuyPotion.Play();
         }
     }
 }

CoinSystem Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 using UnityEngine;
 
 public class CoinSystem : MonoBehaviour {
 
     public int Coins;
     public Text CoinText;
 
     // Use this for initialization
     void Start () {
         Coins = 0;
         UpdateCoins();
     }
     
     public void AddCoins(int CoinsToAdd)
     {
         Coins += CoinsToAdd;
         CoinText.text = "Coins: " +   Coins;
     }
 
     public void SubtractCoins(int CoinsToSubtract)
     {
         if (Coins - CoinsToSubtract < 0)
         {
             CoinsToSubtract = 0;
             UpdateCoins();
             Debug.Log("You Didn't Have Enough Money!");
 
         }
         if (Coins - CoinsToSubtract >= 0)
         {
             Coins -= CoinsToSubtract;
             UpdateCoins();
             Debug.Log("Got Rid Of Coins");
             return;
         }
     }
     void UpdateCoins()
     {
         CoinText.text = "Coins: " + Coins;
     }
 }

Thankyou for taking the time to help me!

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 NoseKills · Sep 28, 2017 at 06:28 AM 0
Share

So what is happening and what would you like to happen ins$$anonymous$$d? $$anonymous$$nowing that would make it easier to help.

The only thing that jumps out to me is that SubtractCoins() has 2 consecutive if clauses inside it and you might be entering both of them because you set CoinsToSubtract to 0 in the first, making it more likely you enter the second.

You should make it an if() {} else if() {} if only one of the conditions should be happening when you call the method. And you should not be changing the value of CoinsToSubtract because why would you?

1 Reply

  • Sort: 
avatar image
1

Answer by WitchLake · Sep 28, 2017 at 11:48 AM

Hi here is how I would do. In the ShopSystem :

 public void BuyManaPot(){
    //Test if the player can spend 10 coins then add item
    if(CoinSystem.GetComponent<CoinSystem>().SubtractCoins(10)){
       inventory.AddItem(ManaPotion);
       BuyPotion.Play();
    }
 }

In the CoinSystem :

 public bool SubtractCoins(int CoinsToSubtract){
    if (Coins - CoinsToSubtract < 0){
       Debug.Log("You Didn't Have Enough Money!");
       return false;
    }
 
    Coins -= CoinsToSubtract;
    UpdateCoins();
    Debug.Log("Got Rid Of " + CoinsToSubtract + " Coin(s)");
    return true;
 }


Comment
Add comment · Show 1 · Share
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 Agent27765 · Sep 29, 2017 at 09:11 PM 0
Share

I have already tried that . It says this when I do it. Assets/Scripts/Player Related/CoinSystem.cs(31,13): error CS0127: `CoinSystem.SubtractCoins(int)': A return keyword must not be followed by any expression when method returns void Do you think you have any other Ideas?

Follow this Question

Answers Answers and Comments

116 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with JS script 0 Answers

How can I add the doors sound opening and closing to this script? 1 Answer

How do I get my audio to play once? 1 Answer

c# Script How to create an AudioClip from the file's path location ? 4 Answers

Would it be possible and is it a good idea? [Loading prefabs by name] 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges