Search Unity

(SOLVED)int isn't updating text

Discussion in 'Scripting' started by RevoltGames, Sep 13, 2017.

  1. RevoltGames

    RevoltGames

    Joined:
    Sep 13, 2017
    Posts:
    2
    I'm trying to create a button that spends money on a game I'm working on (this is my first time trying to program so I apologize for any huge errors). I have it so that it shows how much money I have when it starts but when I buy it isn't changing to the new value. Any ideas?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class first : MonoBehaviour
    7. {
    8.     public int money;
    9.     public Text countMoney;
    10.  
    11.  
    12.  
    13.     void Start()
    14.     {
    15.         money=1000000;
    16.         SetMoneyText ();
    17.     }
    18.  
    19.     void update()
    20.     {
    21.         SetMoneyText ();
    22.     }
    23.  
    24.     public void UpgrademilitaryFunc () {
    25.         money = money-50000;
    26.     }
    27.  
    28.     void SetMoneyText ()
    29.     {
    30.         countMoney.text = "money: " + money.ToString ();
    31.     }
    32. }
    33.  
     
  2. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    update should be Update with a capital U.
     
    RevoltGames likes this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I would also suggest you not update your text in Update. Just update it when your player buys something, not every frame if it's not necessary to do so.

    Per your example, why not call the method in UpgrademilitaryFunc after you update the money.
     
  4. game-rules

    game-rules

    Joined:
    Jan 11, 2014
    Posts:
    45
    Your code is not very optimized. You should not call SetMoneyText in Update, it executes this every frame.
    Also, why are you initializing money in Start ? instead, initialize it directly during declaration, so if you want to adjust the starting in Inpesctor, it will take your value setup.

    Instead your script could implement IPointerClickHandler interface.

    Using GameRules extension, Here is the code I could generate in less than a minute:

    Code (CSharp):
    1. using UnityEngine.AI;
    2. using UnityEngine.EventSystems;
    3. using UnityEngine.UI;
    4.  
    5. namespace GameRules
    6. {
    7.     public class Money_Tanks_RuleObjectFlow : MonoBehaviour, IPointerClickHandler
    8.     {
    9.      
    10.         public int money = 1000000;
    11.      
    12.         public Text countMoney;
    13.      
    14.         // Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.
    15.         void Start ()
    16.         {
    17.             // Logic State SetMoneyText
    18.             perform_SetMoneyText();
    19.      
    20.         }
    21.      
    22.         public void OnPointerClick (PointerEventData eventData)
    23.         {
    24.             // Logic State DecreaseMoney
    25.             perform_DecreaseMoney(50000);
    26.             // Logic State SetMoneyText
    27.             perform_SetMoneyText();
    28.      
    29.         }
    30.      
    31.         void perform_DecreaseMoney (int moneySpent)
    32.         {
    33.          
    34.          
    35.             money = money - moneySpent;
    36.         }
    37.      
    38.         void perform_SetMoneyText ()
    39.         {
    40.          
    41.          
    42.             countMoney.text = "money: " + money.ToString ();
    43.         }
    44.  
    45.     }
    46. }


    --
    Game Rules
    Unified Visual Scripting Asset with rules engine
    https://www.assetstore.unity3d.com/en/#!/content/84383
    https://game-rules.net
     
  5. game-rules

    game-rules

    Joined:
    Jan 11, 2014
    Posts:
    45
    Once the script is generated, just add it to your button and drag and drop the Text UI in the inspector.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    While I can agree his code isn't the best designed code, if he has a button, he's fine using the onclick method to tie it in with the inspector.

    I'm also not certain if you're just adding the extra stuff to try and advertise your asset or not, which probably would be a bit of an overkill for someone starting out. lol. :)
     
  7. game-rules

    game-rules

    Joined:
    Jan 11, 2014
    Posts:
    45
    I would say it is both to advertise a bit, and also to help people as I provide the generated code. Actually after having developed two mobile games in the past, I get really upset by always retypying same things. So I created this extension (it took me two years) to speed and make code more maintainable, even if you still need some knowledge with Unity C# API. This extension reuses similar concepts from a software used in banking and insurance sectors that I am used to.
     
  8. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    I see what you did there. ;)
     
  9. game-rules

    game-rules

    Joined:
    Jan 11, 2014
    Posts:
    45
    Yes it helps also to avoid such typos :)
     
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Also - why implement the interface yourself when Button already does? Let me attach this thing that takes click input to a button that....also takes click input?

    Ultimately - off topic. The correct answer given the initial question is to capitalize the U. Given OP's newness to Unity, that should be the end of it :)
     
  11. RevoltGames

    RevoltGames

    Joined:
    Sep 13, 2017
    Posts:
    2
    Thank you!!! I can't believe I didn't notice....