unity3d 我的值不会增加,当我点击按钮

sczxawaw  于 2023-06-06  发布在  其他
关注(0)|答案(1)|浏览(158)

我正在做一个点击器游戏,我遇到了一个问题。在我的游戏中,当你购买物品时,物品的价值会增加。在我的情况下,我这样做了,当你买这个项目,它的工作第一次,但后来它停止工作。你仍然可以购买物品,但物品的价值不会增加(除了你第一次按下它的时候)。
我试着在你点击它的时候放一个bool,当你点击它的时候,它会变成true,然后当值增加的时候,把它变成false。它没有工作。下面是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Buying : MonoBehaviour
{
    Click click;
    public Text boughtCoal;
    public float time = 1f;
    GameManager gameManager;
    public int whatYouWant;
    public float coalBought = 0;
    public float ValueIncrease;
    bool letValueIncrease = false;

    void Start(){
        click = GameObject.Find("Mine").GetComponent<Click>();
        gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
        ValueIncrease = 5;
    }
    public void OnClick(){
            click.money -= gameManager.coalCost;
            ValueIncrease = Mathf.Round(ValueIncrease * -10f) * 0.01f;
            coalBought += 1;
            letValueIncrease = true;
        
    }
    void Update(){
        time -= Time.deltaTime;
        if(time <= 0){
            click.money += coalBought;
            time += 1f;
        }
        boughtCoal.text = "Coal Bought: " + coalBought;
        if(letValueIncrease == true){
            ValueIncrease = gameManager.coalCost * 1.3f;
            letValueIncrease = false;
    }
    
   }
}

这里也是'GameManager'和'Click'的代码。“游戏管理器”:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{       
        Click click;
        Buying buying;

        public Text coalMiners;
        public Text ironMiners;
        public Text copperMiners;
        public Text goldMiners;
        
        public float coalCost;
        public float ironCost;
        public float copperCost;
        public float goldCost;

        public Button coalB;
        
    // Start is called before the first frame update
    void Start()
    {
        coalCost = 5;
        click = GameObject.Find("Mine").GetComponent<Click>();
        buying = GameObject.Find("Button").GetComponent<Buying>();
    }

    // Update is called once per frame
    void Update()
    {
        coalMiners.text = "Coal Miners: $" + buying.ValueIncrease;
        if(click.money < buying.ValueIncrease){
            coalB.interactable = false;
        }else{
            coalB.interactable = true;
        }
    }
}

最后是一个点击:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Click : MonoBehaviour
{   
    public Text moneyT;
    public float money;
    GameManager gameManager;
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
         moneyT.text = "$$$: " + money;
    }

    void OnMouseDown(){
        money += 1;
    }
}
sg24os4d

sg24os4d1#

ValueIncrease变量取决于煤炭成本。但你似乎没有在代码中增加煤炭成本。它总是5,所以你会得到相同的答案,每次你买。
你可以这样做

gameManager.coalCost = gameManager.coalCost * 1.3f;

并在文本中显示CoalCost

coalMiners.text = "Coal Miners: $" + coalCost;

相关问题