The question is answered, right answer was accepted
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!
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?
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;
}
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
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