Search Unity

How to update a "static var"?

Discussion in 'Scripting' started by Khazzack, Dec 11, 2013.

  1. Khazzack

    Khazzack

    Joined:
    Sep 27, 2013
    Posts:
    29
    I'm not sure i'm going about this the right way, but i'll explain anyways. (P.S. this is JS/US)

    Basically I have a script attached to my Player Game Object (Player Counters) which basically counts the coins collected in my game.
    The Coins can be collected by a "pickup item" and for killing an enemy.

    I then have a script which is attached to an empty Game Object which handles my GUI.
    On my GUI script i have a function to get the coinCounter variable from my Player Counter Script, convert it to a string, then display it using GUI.Labels. I mean this works fine and updates when i kill an enemy or pickup a coin and is shown correctly in the GUI HUD.

    But the problem is, I have now made a shopkeeper, When I "activate" him, my HUD disappears and i'm trying to show the CoinCounter in this GUI.

    But if i use the same code from my GUI script (exactly the same code), instead of being the changed code that was displaying on my HUD, it resets back to the default value of the Player Counter script.

    I am assuming this happens because in my PlayerCounters script, coinCounter variable is "static var". But, unless i assign it as a static var and not just a var, it won't let me reference it for my GUI anyway.

    How do i make it so the Value that is being updated and displayed in my GUI script on the HUD is also updated and kept for the shopkeeper gui?

    I hope that makes sense?

    If you have no idea, and will need to see my code, then i'll post it.

    Or is there a better way for me to do this?

    Thanks
     
    Last edited: Dec 11, 2013
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    You should probably give some examples of your script...

    Code (csharp):
    1.  
    2. public class Player : MonoBehaviour
    3. {
    4.     private static int coins = 0;
    5.  
    6.     public static int Coins
    7.     {
    8.         get { returns coins; }
    9.         set { coins = Mathf.Clamp(value, 0, int.MaxValue; }
    10.     }
    11. }
    12.  
    13. public class MyGUI : MonoBehaviour
    14. {
    15.     private void OnGUI()
    16.     {
    17.         GUI.Label(new Rect(0, 0, 128, 128), Player.Coins);
    18.     }
    19. }
    20.  
    21. public class MyVendor : MonoBehaviour
    22. {
    23.     private void OnGUI()
    24.     {
    25.         GUI.Label(new Rect(0, 0, 128, 128), Player.Coins);
    26.     }
    27. }
    28.  
    This should work properly... You should check that nobody isn't reseting the field when you activate your vendor.
     
  3. Khazzack

    Khazzack

    Joined:
    Sep 27, 2013
    Posts:
    29
    Well, the thing is, i'm using Java/UnityScript not c#, so this would not work for me ^^

    But here is my WorldGUI script...

    Code (csharp):
    1. function OnGUI()
    2. {
    3.     if(toggleGUI == true)
    4.     {  
    5.  
    6.         var coinNumberName = PlayerCounters.coinCounter;
    7.         var coinName = coinNumberName.ToString();
    8.                
    9.         var healthNumberName = PlayerCounters.playerHealth;
    10.         var healthName = healthNumberName.ToString();
    11.                
    12.         var enemyNumberName = PlayerCounters.enemyCounter;
    13.         var enemyName = enemyNumberName.ToString();
    14.  
    15.         GUI.Box(Rect(3,3,140,70), "");
    16.         GUI.Label(Rect(65,5,50,25), healthName);
    17.         GUI.Label(Rect(10,5,50,50), "Health =");
    18.        
    19.         GUI.Label(Rect(65,25,50,50), coinName);
    20.         GUI.Label(Rect(10,25,50,50), "Gold  =");
    21.        
    22.         GUI.Label(Rect(115,45,50,50), enemyName);
    23.         GUI.Label(Rect(10,45,150,50), "Enemies Slain =");
    24.     }
    25. }
    And what i'm trying to do is, use the coinName and healthName variables in my Shop script. It says to add "static" but when i do that under the OnGUI function, i get more errors, and if i place these variables outside of the OnGUI function, they don't update accordingly. I'm lost :p
     
    Last edited: Dec 11, 2013
  4. Khazzack

    Khazzack

    Joined:
    Sep 27, 2013
    Posts:
    29
    bump, anyone got any idea's please? :D