争论不按预期工作

时间:2014-11-21 16:17:19

标签: c# unity3d

我正在尝试制作一些改变变量的按钮,所以每次我必须创建一个新按钮时,我都会创建一个可以为我做的功能。

方法"无效按钮"有4个参数,但是" numberOF"论证不起作用。当我点击按钮时,它应该改变争论" numberOf"和"价格",但它永远不会改变" 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;
    }

}

1 个答案:

答案 0 :(得分:1)

我相信你说这个函数不会修改“numberOf”的值。 这不是严格意义上的,但是您正在做的是修改现在的本地值。这样做的原因是你传入一个int,它不是通过引用传递的,而是通过值传递的。

您可以通过将numberOf修改为ref numberOf来纠正此问题,但我实际上建议尽可能返回numberOf。理解发生的事情更直接,更清晰。

我还建议buttons使用更好的变量和方法名称,因为它并不清楚buttons应该执行什么操作。

有关在C#中使用ref参数的更多信息this article非常好。