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 bgreal5 · Apr 18, 2014 at 07:26 PM · javascriptupdatechangeongui

Variables in GUI not updating/changing? (javascript)

I'm working in javascript and attempting to make a very simple battle system. The issue is, I have to declare the players' and monsters' health in an if statement (because there are different player classes and monsters), but whenever I do this once the calculations in the if statement are done (monsterHealth -= playerAttack) the monster health doesn't update because the condition of the if statement (the current monster's original stats) is still active. Does anyone know how I might fix this? (the problem happens both with the playerHealth and the monsterHealth).

This is the class for the monster stats:

 #pragma strict
 
 public class MonsterData extends MonoBehaviour
     {
         static public var monsterHealth : int;
           static public var monsterArmor: int;
         static public var monsterAttack : int;
           static public var curMonster : String;
         static public var totDamage1 : int = PlayerStats.strength + PlayerStats.weaponDamage1 - monsterArmor;
            static public var totDamage2 : int = PlayerStats.strength + PlayerStats.weaponDamage2 - monsterArmor;
            static public var monNum : Monster = new Monster(0);
            
         public class Monster
         {
                
             public function Monster(t : int)
                    {    
                    
                     totDamage1 = PlayerStats.strength + PlayerStats.weaponDamage1 - monsterArmor;
                     totDamage2 = PlayerStats.strength + PlayerStats.weaponDamage2 - monsterArmor;
         
             if(t == 1)
                 {
                     curMonster = "Mutated Wolf";
                     monsterHealth = 16;
                     monsterArmor = 2;
                     monsterAttack = (Random.Range(2, 4));
                 }
             }
         }
 }


And this is the GUI that runs the battle:

 if(GameGUI.gamestage == -201)
     {
         if(GameGUI.backNum == 4)
         {
             GUI.skin = nightText;
         }
         else
         {
             GUI.skin = text;
         }
         
         GUI.Box(Rect(Screen.width/2 - 250, Screen.height/2 - 300, 500, 500), Mon.curMonster);
         GUI.Box(Rect(Screen.width/2 - 250, Screen.height/2 - 250, 500, 500), "" + Mon.monsterHealth);
         
         GUI.Box(Rect(Screen.width/2 - 250, Screen.height/2 + 200, 500, 500), "Me");
         GUI.Box(Rect(Screen.width/2 - 250, Screen.height/2 + 250, 500, 500), "" + PlayerStats.playerHealth);
         
         if(GUI.Button(Rect(Screen.width/2 - 250, Screen.height/2 - 100, 500, 35), PlayerStats.attack1))
         {
             Mon.monsterHealth -= Mon.totDamage1;
             PlayerStats.playerHealth = PlayerStats.playerHealth - Mon.monsterAttack;
             Mon.monsterAttack = (Random.Range(2, 4));
         }
         if(GUI.Button(Rect(Screen.width/2 - 250, Screen.height/2 - 50, 500, 35), PlayerStats.attack2))
         {
             Mon.monsterHealth -= Mon.totDamage2;
             PlayerStats.playerHealth = PlayerStats.playerHealth - Mon.monsterAttack;
             Mon.monsterAttack = (Random.Range(2, 4));
         }
         
         if(Mon.monsterHealth <= 0)
         {
             Mon.monsterHealth = 0;
             Debug.Log("0");
         }
         
         if(PlayerStats.playerHealth == 0)
         {
             GameGUI.gamestage = -3;
         }
         
     }
Comment
Add comment · Show 4
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 chufraise · Apr 18, 2014 at 08:09 PM 0
Share

Hi bgreal5! Is there any particular reason why you have nested classes and static variables? Static variables can be a pain to work with if you are not sure how they work. If I understand you correctly you have several instances of $$anonymous$$onster, right? Since the variables are static they don't really belong to the instance but to the class. So there is only one monsterHealth variable in the entire application domain. When you change the value, you don't isolate the change to a single instance.

But if there's a good reason for using statics that I didn't grasp from your question, I apologise.

avatar image bgreal5 · Apr 19, 2014 at 01:51 AM 0
Share

I was using static because the GUI that runs the battle is in a different script and I can't access the variables from the battle runner if I don't make them static. Would you suggest I move the GUI runner to the same script as the monster class so I don't have to deal with static?

avatar image chufraise · Apr 19, 2014 at 09:13 AM 0
Share

No, don't do that. You should seperate them completely.

Ok, so you have a bunch of $$anonymous$$onster instances that you want to perform actions on from you GUI. You have some alternatives to accomplish this.

First of all, lose the statics and the nested class.

If you have set number of monsters the easiest way is to declare a public array of monsters in you GUI class. Then you can just drag and drop your monster GameObjects from the hierarchy to the array on the GUI.

Another alternative is to work with delegates and events. You'll get cleaner and more abstract code.

If you are spawning $$anonymous$$onsters you should check out object pooling. Simply instantiate say 50 monster at startup, and spawn and despawn using SetActive(false) and SetActive(true).

Good luck!

avatar image bgreal5 · Apr 20, 2014 at 08:18 PM 0
Share

Thanks! I'll be sure to try it out and come back to update with results!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by BenDurch · Apr 19, 2014 at 12:50 AM

this works for me.

 var Bug : GameObject;
 var Boom : Transform;
 var dammage : int = 10;
 var health : float = 100;
 private var Dead : boolean;
 function OnTriggerEnter2D (col : Collider2D) 
     {
     children = GetComponentsInChildren.<Renderer>();
     if (col.tag == "Bullet"){
         health+=-dammage;
         }
         }
 function Update (){
 if (health<0){
 Dead = true;
 }
 if (Dead==true){
 Destroy(Bug,0);
 }
 }
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

22 People are following this question.

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

Related Questions

Simple quest system: from Update function to OnGUI? (javascript) 0 Answers

problem deforming mesh colliders 1 Answer

Eliminating input loss 1 Answer

GUI opacity. 1 Answer

Unity Controller Support Help 0 Answers


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