我的价值在我点击按钮时不会增加。

huangapple go评论63阅读模式
英文:

My value won't increase when I click on button

问题

以下是您的代码的翻译部分:

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;
        }
    }
}
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;
    }
}

请注意,我已经将HTML实体(如&quot;)翻译为相应的引号字符。希望这有助于您理解代码。

英文:

I am making a clicker game and I encountered a problem. In my game when you buy something the value of the item will increase. In my case I did this and when you buy this item it works the first time but then it stops working. You could still buy the item but the value of the item does not increase (except for when you press it the first time).

I tried to just put a bool when you clicked on it and when you clicked on it, it will turn true and then when the value increased put it to false. It did not work.
Here is my code:

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(&quot;Mine&quot;).GetComponent&lt;Click&gt;();
        gameManager = GameObject.Find(&quot;GameManager&quot;).GetComponent&lt;GameManager&gt;();
        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 &lt;= 0){
            click.money += coalBought;
            time += 1f;
        }
        boughtCoal.text = &quot;Coal Bought: &quot; + coalBought;
        if(letValueIncrease == true){
            ValueIncrease = gameManager.coalCost * 1.3f;
            letValueIncrease = false;
    }
    
   }
}

Here is also the code for 'GameManager' and 'Click'.
'GameManager':

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(&quot;Mine&quot;).GetComponent&lt;Click&gt;();
        buying = GameObject.Find(&quot;Button&quot;).GetComponent&lt;Buying&gt;();
    }

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

Finally here is the one for click:

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 = &quot;$$$: &quot; + money;
    }

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

答案1

得分: 2

ValueIncrease变量依赖于煤炭成本。但是在代码中,似乎没有增加煤炭成本。它始终是5,所以每次购买时你都会得到相同的答案。

你可以这样做:

gameManager.coalCost = gameManager.coalCost * 1.3f;

并在文本中显示CoalCost:

coalMiners.text = "煤矿工人: $" + coalCost;
英文:

The ValueIncrease variable is dependent on Coal cost. But you don't seem to increase the coal cost in the code. Its always 5, so you will get the same answer every time you buy.

You can just do this

gameManager.coalCost = gameManager.coalCost * 1.3f;

And display the CoalCost in the text

coalMiners.text = &quot;Coal Miners: $&quot; + coalCost;

huangapple
  • 本文由 发表于 2023年6月6日 12:31:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定