Unity点击器游戏购买后物品价值无法递增问题求助
点击器游戏购买价格递增失效问题
我正在开发一款点击器游戏,设定购买物品时价格会递增,但实际只有首次购买时逻辑生效,后续能购买物品但价格不再上涨。尝试用布尔变量控制价值递增流程,未能解决问题。相关代码如下:
Buying 脚本
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 脚本
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; } } }
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 = "$$$: " + money; } void OnMouseDown(){ money += 1; } }
问题根源
- 核心变量未更新:所有价格逻辑依赖
gameManager.coalCost,但你从未修改这个变量,仅操作了ValueIncrease,导致价格一直停留在初始值5。 - 冗余逻辑干扰:
letValueIncrease布尔变量完全多余,将价格更新延迟到Update执行,反而造成逻辑混乱。 - 错误计算逻辑:
OnClick里的ValueIncrease = Mathf.Round(ValueIncrease * -10f) * 0.01f;是取反后四舍五入,完全不符合价格递增需求。
修复后的代码
Buying 脚本
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 float coalBought = 0; void Start(){ click = GameObject.Find("Mine").GetComponent<Click>(); gameManager = GameObject.Find("GameManager").GetComponent<GameManager>(); } public void OnClick(){ // 扣除当前购买价格 click.money -= gameManager.coalCost; // 增加已购买数量 coalBought += 1; // 更新下一次购买价格(上涨30%并取整) gameManager.coalCost = Mathf.Round(gameManager.coalCost * 1.3f); } void Update(){ time -= Time.deltaTime; if(time <= 0){ click.money += coalBought; time += 1f; } boughtCoal.text = "Coal Bought: " + coalBought; } }
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; void Start() { coalCost = 5; click = GameObject.Find("Mine").GetComponent<Click>(); buying = GameObject.Find("Button").GetComponent<Buying>(); } void Update() { // 直接显示当前购买价格 coalMiners.text = "Coal Miners: $" + coalCost; // 根据余额判断按钮可用性 if(click.money < coalCost){ coalB.interactable = false; }else{ coalB.interactable = true; } } }
Click 脚本(仅整理格式)
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Click : MonoBehaviour { public Text moneyT; public float money; void Update() { moneyT.text = "$$$: " + money; } void OnMouseDown(){ money += 1; } }
修复说明
- 移除冗余的
ValueIncrease和letValueIncrease,直接操作核心变量gameManager.coalCost。 - 在
OnClick中直接完成价格递增计算,逻辑更直接,避免延迟执行的混乱。 - 修正价格计算逻辑,用
Mathf.Round(gameManager.coalCost * 1.3f)实现每次购买后价格上涨30%并取整。 - 统一所有价格相关逻辑依赖
coalCost,确保显示和判断逻辑一致。
内容的提问来源于stack exchange,提问作者gamingpcgamer
相关产品推荐
相关产品推荐

