Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Can't update my variables data on a GUI call

Discussion in 'Scripting' started by henmachuca, Nov 1, 2016.

  1. henmachuca

    henmachuca

    Joined:
    Oct 14, 2016
    Posts:
    95
    Hi, I am trying to create an allocation stat system.
    For that, I created a StatAllocation script where I set the attributes I want to be able to edit and I create a reference to this script in my player Stats script so every time the player clicks to see the stats window he will be able to distribute points if he has any.
    My two problems are:

    - The StatAllocation is getting the variables as they start in the Player_Stats script but once I update them in the editor to test them out, it does not updates in the GUI;
    - I would like to call the OnGUI() method only when the player opened the Stats Window, but right now the function is called multiple times every frame.

    Below is the code for the StatAllocation Script and where it is being called

    STATSALLOCATION Script
    Code (CSharp):
    1. public class StatAllocationModule
    2. {
    3.     private Player_Stats playerStatsScript = new Player_Stats();
    4.  
    5.     private string[] statNames = new string[5] {"Strength", "Endurance", "Agility", "Wisdom", "Intellect" };
    6.     private string[] statDescription = new string[5] {"Damage modifier", "HP modifier", "Speed modifier", "Gift modifier", "Mage modifier" };
    7.     private bool[] statSelection = new bool[5];     //control the toggle switch used in the for loop
    8.  
    9.     private int[] pointsToAllocate = new int[5];    //starting stat values, but used to modify
    10.     private int[] baseStatPoints = new int[5];      //starting stat values
    11.  
    12.     public void DisplayStatAllocationModule()  {
    13.         RetrieveBaseStatPoints();
    14.         DisplayStatToggleSwitches(); ; //call the heart and soul of this function
    15.         DisplayStatIncreaseDecreaseButtons();
    16.     }
    17.  
    18.     private void DisplayStatToggleSwitches()  {
    19.         for (int i = 0;  i < statNames.Length; i++)   {
    20.             statSelection[i] = GUI.Toggle(new Rect(10,60*i + 10,100,50), statSelection[i], statNames[i]);
    21.             GUI.Label(new Rect(100, 60*i + 10, 50, 50), pointsToAllocate[i].ToString());
    22.             if (statSelection[i])   {
    23.                 GUI.Label(new Rect(20, 60*i + 30, 150, 100), statDescription[i]);
    24.             }
    25.         }
    26.     }
    27.  
    28.     public void DisplayStatIncreaseDecreaseButtons()  {
    29.         for(int i = 0; i < pointsToAllocate.Length; i++)   {
    30.             if (pointsToAllocate[i] >= baseStatPoints[i] && playerStatsScript.statPoints > 0)  {
    31.                 if (GUI.Button(new Rect(200, 60 * i + 10, 50, 50), "+"))  {
    32.                     pointsToAllocate[i] += 1;
    33.                     --playerStatsScript.statPoints;
    34.                 }
    35.             }
    36.             if (pointsToAllocate[i] > baseStatPoints[i])  {
    37.                 if (GUI.Button(new Rect(260, 60 * i + 10, 50, 50), "-"))  {
    38.                     pointsToAllocate[i] -=  1;
    39.                     ++playerStatsScript.statPoints;
    40.                 }
    41.             }
    42.         }
    43.     }
    44.  
    45.     private void RetrieveBaseStatPoints()
    46.     {
    47.         pointsToAllocate[0] = playerStatsScript.playerStrength;
    48.         pointsToAllocate[1] = playerStatsScript.playerEndurance;
    49.         pointsToAllocate[2] = playerStatsScript.playerAgility;
    50.         pointsToAllocate[3] = playerStatsScript.playerWisdom;
    51.         pointsToAllocate[4] = playerStatsScript.playerIntelligence;
    52.         baseStatPoints[0] = playerStatsScript.playerStrength;
    53.         baseStatPoints[1] = playerStatsScript.playerEndurance;
    54.         baseStatPoints[2] = playerStatsScript.playerAgility;
    55.         baseStatPoints[3] = playerStatsScript.playerWisdom;
    56.         baseStatPoints[4] = playerStatsScript.playerIntelligence;
    57.     }
    PLAYER STATS Script

    Code (CSharp):
    1. public class Player_Stats : MonoBehaviour
    2. {
    3.     public int playerStrength = 15;
    4.     public int playerEndurance= 15;
    5.     public int playerAgility= 15;
    6.     public int playerWisdom = 15;
    7.     public int playerIntelligence = 15;
    8. ...
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,658
    Any reason you are using the legacy system and not the new UI? This is so much easier to do and nicer in the new UI system.
     
  3. henmachuca

    henmachuca

    Joined:
    Oct 14, 2016
    Posts:
    95
    No particular reason! I can update it to the new UI system afterwards, but the current one is working fine. My problem is that it is not getting the values for player stats (playerStrength, playerEndurance, etc). This is what I cannot figure how to solve.
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,588
    Main problem:
    You obviously want your Player_Stats instance to be attached to a GameObject, which you've obviously done (as you try to change values via inspector).

    But in your module script, you create an instance with the constructor. First of all, they are 2 completely different instances and secondly, MonoBehaviours are not supposed to be instantiated using the constructor, only by adding them via inspector or with AddComponent.

    Something about the approach:
    The arrays that you're using might be better bundled to a type, as the name, description etc appear to belong together. You'd end up having ONE array of a complex type, not 5 arrays for each value of one "Stat".

    Also, I'd rather make the module a MonoBehaviour and the Player_Stats class Serializable. That's more intuitive, but that's just my preference. :)
     
    henmachuca likes this.
unityunity