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 Finstern · Jul 02, 2012 at 10:45 PM · guiarray

Working on a battle system, GUI not updating with newest values...

Here is my battle.js code:

 var currentGUIMethod : Function;//Which GUI we are using
 
 function Start () {
  this.currentGUIMethod = mainMenu;
 }
 
 function Update () {
 
 }
 
 function mainMenu(){
  GUILayout.BeginArea(Rect(Screen.width * 0.25, Screen.height * 0.025, Screen.width * 0.5, Screen.height * 0.95));//Begin GUI Area
  GUILayout.Window (0, Rect (Screen.width * 0.01, Screen.height * 0.69, Screen.width * 0.98, Screen.height * 0.3), Menu, "Main Menu");// Window ID, Dimensions, Function, Title
  GUILayout.EndArea();//End GUI Area
 }
 
 function Menu (){ 
  var player : Player;
  for(player in Game.playerList){ 
  GUILayout.BeginHorizontal();
  GUILayout.BeginVertical ("box");
  if(GUILayout.Button ("Attack")) {
  player.playerHealth = player.playerHealth - player.playerStrength;
  }
  if(GUILayout.Button ("Skills")){
  
  }
  if(GUILayout.Button ("Summon")){
  
  }
  if(GUILayout.Button ("Items")) {
  
  }
  GUILayout.EndVertical ();
  GUILayout.BeginVertical("box");
  
  GUILayout.Label(player.playerName);
  GUILayout.Label((player.playerHealthString)+"/"+(player.playerMaxHealthString));
  GUILayout.Label((player.playerActionPointsString)+"/"+(player.playerMaxActionPointsString));
  }
  // GUILayout.Label(Game.playerCharacter.playerName);
  // GUILayout.Label(Game.playerCharacter.playerPortrait);
  // GUILayout.Label((Game.playerCharacter.playerHealthString)+(Game.playerCharacter.playerMaxHealthString));
  // GUILayout.Label((Game.playerCharacter.playerActionPointsString)+(Game.playerCharacter.playerMaxActionPointsString));
  GUILayout.EndVertical();
  GUILayout.EndHorizontal();
 }
 
 function OnGUI () {
  this.currentGUIMethod();//Draw the current GUI
 }

Here is the script containing the player values:

 static var playerCharacter : Player;
 static var playerList = Array();
 var tempPlayer = Player();
 
 function Awake () {
  createPlayerList();
     DontDestroyOnLoad (transform.gameObject);
 }
 
 function Update () {
 // player = GameObject.Find("Player");
 // playerPos = player.transform.position;
  //Debug.Log(itemList.length); 
  if(playerCharacter.playerHealth <= 0){
  Debug.Log("Game Over");
  }
 }
 
 function createPlayerList (){
  playerCharacter = Player();
  playerCharacter.player = GameObject.Find("Player");
  playerCharacter.playerPortrait = Resources.Load ("Textures/player/portrait", Texture);
  playerCharacter.playerBattleTexture = Resources.Load ("Textures/player/battletexture", Texture);
  playerCharacter.playerName = "Clare";
  playerCharacter.playerHealth = 150;
  playerCharacter.playerMaxHealth = 150;
  playerCharacter.playerActionPoints = 50;
  playerCharacter.playerMaxActionPoints = 50;
  playerCharacter.playerStrength = 5;
  playerCharacter.playerDefense = 6;
  playerCharacter.playerKarma = 0;
  playerCharacter.playerMoney = 500;
  playerCharacter.playerHealthString = playerCharacter.playerHealth.ToString();
  playerCharacter.playerMaxHealthString = playerCharacter.playerMaxHealth.ToString();
  playerCharacter.playerActionPointsString = playerCharacter.playerActionPoints.ToString();
  playerCharacter.playerMaxActionPointsString = playerCharacter.playerMaxActionPoints.ToString();
  playerCharacter.playerKarmaString = playerCharacter.playerKarma.ToString();
  playerCharacter.playerMoneyString = playerCharacter.playerMoney.ToString();
  playerList.Push(playerCharacter);
 }

When I click attack it deals the damage to the playerCharacter, I know this because when the playerCharacter's health reaches 0 the Console prints "Game Over"

What I'm wondering (and cant seem figure out) is why the GUI doesn't update to reflect the player's health everytime?

Can you see what's going wrong?

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 Finstern · Jul 03, 2012 at 10:21 AM 0
Share

I've tried running the for loop in update and setting the gui labels to variables which = the array vars, on the off chance that the array values aren't being updated in the Battle.js script, but that didn't help, I'm convinced its the gui not updating.

1 Reply

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

Answer by Finstern · Jul 03, 2012 at 10:32 AM

Solved!

On creating and submitting the player information to the Array in the player script, I was telling it:

  playerCharacter.playerHealthString = playerCharacter.playerHealth.ToString();

This seems to have been what was keeping the values the same, it wasn't updating with the Battle.js script

So, what I did was:

for(player in Game.playerList){ displayPlayerName = player.playerName; displayPlayerHealth = player.playerHealth.ToString(); displayMaxPlayerHealth = player.playerMaxHealthString; displayPlayerActionPoints = player.playerActionPointsString; displayMaxPlayerActionPoints = player.playerMaxActionPointsString; }

So instead of relying on old information in the array that wasn't being updated, I told it to convert the string on the fly in the Battle.js script

=D

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can't destroy an arrray with GUI Text 1 Answer

How to output a file to a single string? 1 Answer

Gathering GameObjects and then creating a button for each GameObject?? 2 Answers

Arrange ints from biggest to smallest and display them in a table 1 Answer

Creating a GUI Label List from Array 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