2014-11-21 6 views
0

변수를 변경하는 버튼을 만들려고합니다. 따라서 새 버튼을 만들 때마다 새 코드를 작성하는 대신 저를 위해 할 수있는 함수를 만들었습니다.인수가 의도 한대로 작동하지 않습니다.

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

} 
+0

여기에 질문이 없습니다. 도움에 대한 광범위한 요청. 너 뭐가 붙어 있니? – techvice

+0

"사용하지 않는 것들이 많이 있습니다."- 관련성 높은 부분 만 표시하도록 코드를 수정하십시오. "참조로 전달"을 읽을 필요가 있을지 모르지만 다음과 같이 말하기는 어렵습니다. 당신이 게시 한 코드 벽에 정말 문제가됩니다. –

+0

** 여자 친구 오류 405 : 의도적으로 작동하지 않는 인수 ** : D – LearnCocos2D

답변

1

저는이 함수가 "numberOf"값을 수정하지 않는다고 말합니다. 이것은 사실은 아니지만 현재 수행중인 로컬 값을 수정하는 것입니다. 그 이유는 참조로 전달 된 것이 아니라 값으로 전달 된 int를 전달하고 있기 때문입니다.

numberOf를 ref numberOf으로 수정하여이 문제를 해결할 수도 있지만 가능한 경우 numberOf 만 반환하는 것이 좋습니다. 일어난 일을 이해하는 것은보다 직접적이고 명확합니다.

또한 buttons에 대한 더 나은 변수 및 메소드 이름을 권장합니다. 이는 수행 할 작업 인 buttons이 모두 명확하지 않기 때문입니다.

C# this article에서 ref 인수를 사용하는 방법에 대한 자세한 내용은 매우 좋습니다.

+0

정확함, 할당 할 이유가 없습니다. Update() 함수에서의 변수 자체. "Buy"if/else는 똑같은 코드 세그먼트를 포함하고 있기 때문에 단일 if로 단축 될 수 있습니다. –

+0

실험을 통해 도움이되는지 확인하고 C#에 다소 익숙합니다. –

관련 문제