参数未按预期工作
本文关键字:工作 参数 | 更新日期: 2023-09-27 18:31:29
我正在尝试制作一些更改变量的按钮,因此我不是每次必须创建新按钮时都编写新代码,而是创建了一个可以为我执行此操作的函数。
"void buttons"方法有4个参数,但"numberOF"参数不起作用。当我点击按钮时,它应该更改参数"numberOf"和"price",但由于某种原因它永远不会更改"numberOf"。
希望这里有人可以帮助我。我知道有很多东西我没有用过。 当我第一次尝试这个时,我每次都用长代码做按钮,所以仍然需要清理一些无关的代码。
using UnityEngine;
using System.Collections;
public class Gui : MonoBehaviour
{
public int one;
//Money
public int money;
//Iventory
public int stone;
public int jord;
public int frø;
public int korn;
public int brød;
//Item price
public int stonePrice;
public int jordPrice;
public int frøPrice;
public int kornPrice;
public int brødPrice;
//Item inventory text and button text
private string moneyText;
private string stoneText;
private string jordText;
private string frøText;
private string kornText;
private string brødText;
private string stoneButton;
private string jordButton;
private string frøButton;
private string kornButton;
private string brødButton;
//Screen heith/width = 0
private int screenWidth;
private int screenHeight;
// Runs one time
void Start ()
{
//Set variables to screen heith/width to 0
screenWidth = Screen.width - Screen.width;
screenHeight = Screen.height - Screen.height;
//Money reset
money = 10000;
//inventory reset
stone = 0;
jord = 0;
frø = 0;
korn = 0;
brød = 0;
//item price reset
stonePrice = 1;
jordPrice = 3;
frøPrice = 5;
kornPrice = 7;
brødPrice = 9;
//Set item text
moneyText = "money: " + money + " kr.";
stoneText = " stone " + stone;
jordText = " jord " + jord;
frøText = " frø " + frø;
kornText = " korn " + korn;
brødText = " brød " + brød;
//set button text
stoneButton = "Stone " + stonePrice + " kr.";
jordButton = "Jord " + jordPrice + " kr.";
frøButton = "Frø " + frøPrice + " kr.";
kornButton = "Korn " + kornPrice + " kr.";
brødButton = "Brød " + brødPrice + " kr.";
}
void Update ()
{
stone = stone;
jord = jord;
frø = frø;
korn = korn;
brød = brød;
moneyText = moneyText;
stoneText = stoneText;
jordText = jordText;
frøText = frøText;
kornText = kornText;
brødText = brødText;
//Check item text changes
moneyText = "money: " + money + " kr.";
stoneText = " stone " + stone;
jordText = " jord " + jord;
frøText = " frø " + frø;
kornText = " korn " + korn;
brødText = " brød " + brød;
//Check button text changes
stoneButton = "Stone " +stonePrice + " kr.";
jordButton = "Jord " + jordPrice + " kr.";
frøButton = "Frø " + frøPrice + " kr.";
kornButton = "Korn " + kornPrice + " kr.";
brødButton = "Brød " + brødPrice + " kr.";
}
void OnGUI ()
{
buttons(150, stoneButton, stone, stonePrice);
if (GUI.Button (new Rect (Screen.width - 100, Screen.height - 20, 100, 20), "End Turn"))
{
newRound();
}
//Iventory
GUI.TextArea (new Rect (screenWidth + 1, screenHeight + 2, Screen.width, 20),moneyText + " Inventory: " + stoneText + jordText + frøText + kornText + brødText);
}
void make_buttons(int position_heigth, string buttonText, int numberOF, int price)
{
GUI.TextArea (new Rect (screenWidth + 2, screenHeight + position_heigth + 80, 80, 20), buttonText);
if (GUI.Button (new Rect (screenWidth + 80, screenHeight + position_heigth + 80, 40, 20), "Buy"))
{
if (money > price)
{
numberOF = 1 + numberOF;
money = money - price;
}
else if (money == price)
{
numberOF = 1 + numberOF;
money = money - price;
}
}
if (GUI.Button (new Rect (screenWidth + 120, screenHeight + position_heigth + 80, 40, 20), "Sell"))
{
if (numberOF > 0)
{
numberOF = numberOF - 1;
money = money + price;
}
}
}
void newRound ()
{
stonePrice = stonePrice * 2;
jordPrice = jordPrice * 2;
frøPrice = frøPrice * 2;
kornPrice = kornPrice * 2;
brødPrice = brødPrice * 2;
}
}
我相信你是说该函数不会修改"numberOf"的值。这并不完全正确,但是您正在做的是修改现在的本地值。这样做的原因是你传递了一个 int,它不是通过引用传递的,而是通过值传递的。
您可以通过将 numberOf 修改为 ref numberOf
来更正此问题,但我实际上建议如果可能的话只返回 numberOf。更直接、更清晰地了解发生了什么。
我还建议buttons
更好的变量和方法名称,因为根本不清楚buttons
应该执行什么操作。
有关在 C# 中使用 ref 参数的更多信息,本文非常好。