Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by McDardy · Aug 28, 2012 at 11:49 AM · guiupdatevariablesimporting

GUI player menu is not updating values (score, exp, HPs)

GUI player menu is not updating values (score, exp, HPs) after I added Hit Points and Exp to character (name of prefab:Player (yeah, I know - how original)). I'll try to describe everything, but the line that's generates problem (not error - everything is working but not as I designed) is in second line of code beneath point six:

One. (number list makes formating here weird and is messing with code view) I created cube prefab (name:Cube);

Two. I created script that I added to my player (tag: "Player" (PlayerStatus.js):

 public var actualHealth     : int = 100;
 public var maximumHealth     : int = 100;
 public var lives             : int = 3;
 public var actualEXP         : int = 0;
 public var actualLVL        : int = 0;
 
 function ApplyDamage (damage : int)
 {
     actualHealth -=damage;
 
     if (actualHealth<= 0)
     {
         SendMessage("Die");
         lives --;
     }
 }
 
 function ApplyEXP (exp : int)
 {
     actualEXP +=exp;
     
     if(actualEXP >= 100)
     {
         actualEXP -= 100;
         actualLVL++;
     }
 }

Three. I created script that after collision send signal that something happened and attached it to cube prefab (I call it CubeExperience.js - it basically should add exp and take hitpoints by var damage and disappear in thin air):

 public var exp        : int= 45;
 public var damage     : int= 10;
 public var hit         : RaycastHit;
  
 public function OnTriggerEnter (hit : Collider) : IEnumerator 
 {
     if (hit.collider.tag == "Player") 
     {
         hit.collider.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
 
         hit.collider.SendMessage("ApplyEXP", exp, SendMessageOptions.DontRequireReceiver);
         Destroy (this.gameObject);
     }
 }

Four. Everything was right as you can see during gameplay that values of PlayerStatus are changing (and it can be added to anything so it nice and everything, but...). There's always a "but(t)"...

Five. I wanted to implement pause function in my HeroMenu.js and I found it easy though I must disabled player (person beghind keyboard not my prefab) controls because when player presses space or arrow keys, after closing menu hero was jumping which could lead to his death:

 public function Update ()  : void
 {
     if (Input.GetKey(KeyCode.M))
     {
         if (b_openMenu == false)
         {
             //pausing game
             Time.timeScale = 0;
             //opening menu
             b_openMenu = true;
             //disabling player movement script
             var playerMovement = GetComponent("CharacterController_2D");
             playerMovement.enabled = false;
         }
     }
 }
 
 private function DoMyWindow    (windowID    : int)    : void
 {
     in_toolbar = GUI.Toolbar(r_tabButton, in_toolbar, s_toolbars, GUI.skin.GetStyle("Tab Button"));
     
     switch (in_toolbar)
     {
         case 0 : //status page
         StatusWindow();
             break;
         case 1 : //items page
         ItemWindow();
             break;
         case 2 : //equipment page
         EquipmentWindow();
             break;
     }
     GUI.DrawTexture (r_hero, t_hero); //Draw our background character texture
     if (GUI.Button (r_closeButton, "", GUI.skin.GetStyle("Exit Button")))
     {
         b_openMenu = false;
         Time.timeScale = 1;
         var playerMovement = GetComponent("CharacterController_2D");
         playerMovement.enabled = true;
     }
     GUI.DragWindow();
 }

Six. After that, full of enthusiasm I was trying to change both HP, EXP and other RPG like values (score, Attack, etc.) values but after I failed in all I just concentrated my efforts on EXP, so I change HeroMenu.js script (that's my GUI script) (I pasted only the crucial/changed parts) cause I didn't want make any trouble, but I can also paste entire script or send it:

         private var playerInfo : PlayersStatus; 
         private var showEXP = playerInfo.actualEXP;     //<-this line
     //generates NullReferenceException: Object reference not set to an instance of 
     //an object. HeroMenu..ctor () (at Assets/HeroMenu.js:110)
         
         private function StatusWindow() : void
         {
             GUI.Box (r_statBox, "");
             GUI.Box (r_weaponBox, "");
             GUI.DrawTexture(r_statTexture1, t_statusBox1);
             GUI.DrawTexture(r_statTexture2, t_statusBox2);
             GUI.DrawTexture(r_skillBox, t_skillBox);
             
             CheckMax();
             
             GUI.Label (r_hpLabel, currentHP.ToString() + "/" + fullHP.ToString(), "Text Amount");
             GUI.Label (r_mpLabel, currentMP.ToString() + "/" + fullMP.ToString(), "Text Amount");
             GUI.Label (r_lvLabel, currentLV.ToString(), "Text Amount");
         
             GUI.Label (r_expLabel, showEXP.ToString(), "Text Amount");
         
             GUI.Label (r_nextLabel, currentNEXT.ToString(), "Text Amount");
             GUI.Label (r_atkLabel, currentATK.ToString(), "Text Amount");
             GUI.Label (r_defLabel, currentDEF.ToString(), "Text Amount");
             GUI.Label (r_agiLabel, currentAGI.ToString(), "Text Amount");
             GUI.Label (r_intLabel, currentINT.ToString(), "Text Amount");
             GUI.Label (r_lucLabel, currentLUC.ToString(), "Text Amount");
             GUI.Label (r_weaponLabel, gui_weaponCon, "Text Item");
             GUI.Label (r_armorLabel, gui_armorCon, "Text Item");
             GUI.Label (r_accessLabel, gui_accessCon, "Text Item");
             GUI.Label (r_skillTexture, gui_skillCon, "Text Item");
         }
         
         private function CheckMax () : void
         {
             fullHP = Mathf.Clamp(fullHP, 0.0, maxHP);
             fullMP = Mathf.Clamp(fullMP, 0.0, maxMP);
             currentHP = Mathf.Clamp(currentHP, 0.0, fullHP);
             currentMP = Mathf.Clamp(currentMP, 0.0, fullMP);
             currentLV = Mathf.Clamp(currentLV, 0.0, maxLV);
             showEXP = Mathf.Clamp(showEXP, 0.0, maxEXP);
             currentNEXT = Mathf.Clamp(currentNEXT, 0.0, maxNEXT);
             currentATK = Mathf.Clamp(currentATK, 0.0, maxATK);
             currentDEF = Mathf.Clamp(currentDEF, 0.0, maxDEF);
             currentAGI = Mathf.Clamp(currentAGI, 0.0, maxAGI);
             currentINT = Mathf.Clamp(currentINT, 0.0, maxINT);
             currentLUC = Mathf.Clamp(currentLUC, 0.0, maxLUC);
         }

Seven. At some point I even add this to Start() in HeroMenu.js

//if(!playerInfo) //{ // playerInfo = GameObject.Find("Player").GetComponent(PlayersStatus); //}

(but I commented this as it was changing nothing)

Eight. I changed many times many variables I even was trying to sending exp : int directly to HeroMenu.js (private function StatusWindow(exp : int)) but it was generating more errors. I hope that I didn't bore you all but I was trying to describe everything for future references.

Nine. Thank you all for your time.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image McDardy · Aug 28, 2012 at 12:12 PM 0
Share

Here on answers.unity3d I found this: http://answers.unity3d.com/questions/228803/Updating-GUILabel-for-Score.html This is exactly my problem in a little shorter way but those answers are not solving it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by McDardy · Aug 29, 2012 at 06:19 AM

So... the answer found me in person of Jate Wittayabundit (yup! That's right! Author of Unity 3 Game Development Hotshot)

"For your question, you need to get the playerInfo before calling it. Then, when you want to update the EXP number inside the StatusWindow().

Like this : in HeroMenu.js.

     private var playerInfo : PlayersStatus;
     private var showEXP : int = 0;
     
     function Start() 
     {
          playerInfo = GameObject.Find("Player").GetComponent(PlayersStatus); //<-this was added
     }
     
     private function StatusWindow() : void
     {
         // Update the actual EXP
         showEXP = playerInfo.actualEXP; //<- this is making the difference
     
     
         GUI.Box (r_statBox, "");
         GUI.Box (r_weaponBox, "");
         GUI.DrawTexture(r_statTexture1, t_statusBox1);
         GUI.DrawTexture(r_statTexture2, t_statusBox2);
         GUI.DrawTexture(r_skillBox, t_skillBox);
        
         CheckMax();
     
         GUI.Label (r_expLabel, showEXP.ToString(), "Text Amount");
     }

"

I removed all of not necessary lines to not confusing anybody with answer but if something is unclear just comment me for help.

And thanks Jate one more time.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is OnGUI not working? 1 Answer

Unity update destroyed my Scrollview UI. How can I restore it? 0 Answers

I have been struggling with a GUI window with table data for over a week now. What am I doing wrong? 1 Answer

Why is this Pickup Script Not Updating my GUI for ammo 0 Answers

Variable goes very high in update method 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges