Mikkel Nielsen
Mikkel Nielsen

Reputation: 9

Argument not working as intended

I am trying to make some buttons which change variables, so instead of writing new code every time I have to make a new button, I made a function which could do it for me.

The method "void buttons" has 4 arguments, but the "numberOF" argument does not work. When I clicked on the button, it was supposed to change arguement "numberOf" and "price", but it will never change "numberOf" for some reason.

Hopefully there is someone here that can help me. I know there is a lot of stuff I didn't use. I did the buttons with a long code every time when I tried this the first time, so there is some extraneous code still needing to be cleaned out.

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

}

Upvotes: 0

Views: 54

Answers (1)

Ian T. Small
Ian T. Small

Reputation: 298

I believe you are saying that the function does not modify the value of "numberOf". This is not strictly true, however what you are doing is modifying a now local value. The reason for this is you are passing in an int, which is not passed by reference but rather by value.

You could probably correct this by modifying numberOf to ref numberOf, but I would actually recommend just returning numberOf if possible. It's more straight forward and clearer to understand what happened.

I would also recommend better variable and method names for buttons as it's not at ALL clear what action buttons is supposed to perform.

For further information on using ref arguments in C# this article is quite good.

Upvotes: 1

Related Questions