Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Trying to reset variables from within a method doesn't work, but the rest of the method does

Discussion in 'Scripting' started by unity_4r_hs_ak-C7uBg, Feb 26, 2018.

  1. unity_4r_hs_ak-C7uBg

    unity_4r_hs_ak-C7uBg

    Joined:
    Jan 24, 2018
    Posts:
    23
    I have a script that is currently saving information from text boxes to a file. I then am attempting to reset the text boxes back to 0. However, that doesn't seem to be working. I initially had what was in the ResetValues method below in the Save method at the very end, but it still didn't work. So I just adding both the save, and then the resetvalue methods to the onclick event of the same button.
    (The TMP_Text functions the same as the standard Text, I have tested this before. It's from TextMeshPro)

    Code (CSharp):
    1.   using UnityEngine;
    2.     using System.IO;
    3.     using TMPro;
    4.     using System.Runtime.Serialization.Formatters.Binary;
    5.  
    6.     public class SaveGameFootball : MonoBehaviour{
    7.  
    8.         public static int ID;
    9.  
    10.         public TMP_Text Touchdowns;
    11.         public TMP_Text Passes;
    12.         public TMP_Text Yards;
    13.         public TMP_Text FieldGoals;
    14.         public TMP_Text Tackles;
    15.         public TMP_Text Interceptions;
    16.         public TMP_Text FumbleRecoverys;
    17.         public TMP_Text TurnoverOnDowns;
    18.         public TMP_Text TeamScore;
    19.         public TMP_Dropdown GameWon;
    20.         private bool win;
    21.  
    22.         private void Update()
    23.         {
    24.             if(GameWon.value == 1)
    25.             {
    26.                 win = true;
    27.             }
    28.             else
    29.             {
    30.                 win = false;
    31.             }
    32.         }
    33.  
    34.         public void SaveFile()
    35.         {
    36.             ID++;
    37.             string destination = Application.persistentDataPath + "/" + ID.ToString()+ "_Save.dat";
    38.             FileStream file;
    39.  
    40.             if (File.Exists(destination))
    41.             {
    42.                 file = File.OpenWrite(destination);
    43.                 Debug.Log(destination);
    44.             }
    45.             else
    46.             {          
    47.                 file = File.Create(destination);
    48.                 Debug.Log(destination);
    49.             }
    50.             FootballSave data = new FootballSave(Touchdowns.text.ToString(), Passes.text.ToString(), Yards.text.ToString(), FieldGoals.text.ToString(), Tackles.text.ToString(), Interceptions.text.ToString(), FumbleRecoverys.text.ToString(), TurnoverOnDowns.text.ToString(), TeamScore.text.ToString(), win);
    51.             BinaryFormatter bf = new BinaryFormatter();
    52.             bf.Serialize(file, data);
    53.             file.Close();
    54.         }
    55.         public void ResetValues()
    56.         {
    57.             Touchdowns.text = "0";
    58.             Passes.text = "0";
    59.             Yards.text = "0";
    60.             FieldGoals.text = "0";
    61.             Tackles.text = "0";
    62.             Interceptions.text = "0";
    63.             FumbleRecoverys.text = "0";
    64.             TurnoverOnDowns.text = "0";
    65.             TeamScore.text = "0";
    66.             GameWon.value = 0;
    67.         }
    68.     }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    You should be able to call ResetValues() at the end after file.Close();

    From a code side, I'm not seeing anything wrong. If you aren't getting any errors, then something else must be going on.
     
  3. unity_4r_hs_ak-C7uBg

    unity_4r_hs_ak-C7uBg

    Joined:
    Jan 24, 2018
    Posts:
    23
    Yea I just tried calling resetvalues within the save method, still not updating. I guess it's something to do with my setup, but I can't really understand how.
    Edit: Also yes, I am not getting any errors.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Not sure. I'm assuming that your save is being called and working correctly and that all text objects are hooked up. If you have a debug.log in the ResetValues method is it getting printed off at least?
     
  5. unity_4r_hs_ak-C7uBg

    unity_4r_hs_ak-C7uBg

    Joined:
    Jan 24, 2018
    Posts:
    23
    Yea the save is working, the file appears, that seems all good. Text objects all hooked up well too. And there wasn't a debug but I just put one and it is logging that the values were reset.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    So, if you reset the values, then after resetting the values, do this

    Debug.Log(Touchdowns.name, Touchdowns.gameObject);

    Then click on the debug message in the console to make sure it's targeting the object you expect it to. It should flash it in the hierarchy for you.
     
  7. unity_4r_hs_ak-C7uBg

    unity_4r_hs_ak-C7uBg

    Joined:
    Jan 24, 2018
    Posts:
    23
    Alright, so I have a theory. I did what you said, and it pointed to the correct game object. However, the value hadn't changed. Would this be the case if the game object is set to inactive? And if so, what would I do to fix solve that? I would really rather not remove the enabling and disabling of objects, as that would take a huge amount of time. I was under the impression that if a gameobject is set, it can still be modified, even if it's inactive. I may be wrong, but that's my theory for the time being.
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Since you have the reference to the TMP_Text object, it shouldn't matter...however, I actually just noticed, if these are text objects under a canvas, you should try using the TextMeshProUGUI variable type instead

    public TextMeshProUGUI Touchdowns; for example

    I use TMP in one of my projects, and the gui objects are of this type.
     
  9. unity_4r_hs_ak-C7uBg

    unity_4r_hs_ak-C7uBg

    Joined:
    Jan 24, 2018
    Posts:
    23
    Alright so I tried that and nothing seems to change. That is interesting though, as I've been using TMP_Text for my entire project and haven't had anything weird happen. My last theory before I just start it all from scratch or something is the other script I have must be conflicting with it somehow.
    Code (CSharp):
    1. using UnityEngine;
    2. using TMPro;
    3.  
    4. public class MakeAndMissButtonFootball : MonoBehaviour {
    5.  
    6.     public TMP_Text totalPoints;
    7.     //Offense Stats
    8.     public TMP_Text passCompleteText;
    9.     public TMP_Text yardsRushedText;
    10.     public TMP_Text touchdownText;
    11.     public TMP_Text fieldGoalText;
    12.     //Defense Stats
    13.     public TMP_Text tackleText;
    14.     public TMP_Text fumbleRecoveryText;
    15.     public TMP_Text interceptionText;
    16.     public TMP_Text turnoverOnDownsText;
    17.     //Integer Storing
    18.     public int pass, yards, touchdowns, fieldgoals, tackles, fumblerecovery, interceptions, turnoverondowns;
    19.     public int totalScore;
    20.  
    21.     public void Update()
    22.     {
    23.         totalScore = pass + yards + touchdowns + fieldgoals + tackles + fumblerecovery + interceptions + turnoverondowns;
    24.  
    25.         totalPoints.text = totalScore.ToString();
    26.  
    27.         passCompleteText.text = pass.ToString();
    28.         yardsRushedText.text = yards.ToString();
    29.         touchdownText.text = touchdowns.ToString();
    30.         fieldGoalText.text = fieldgoals.ToString();
    31.         tackleText.text = tackles.ToString();
    32.         fumbleRecoveryText.text = fumblerecovery.ToString();
    33.         interceptionText.text = interceptions.ToString();
    34.         turnoverOnDownsText.text = turnoverondowns.ToString();
    35.     }
    36.  
    37.     public void Pass(int worth)
    38.     {
    39.             pass += worth;
    40.         if (pass <= 0)
    41.         {
    42.             pass = 0;
    43.         }
    44.     }
    45.    
    46.     public void Yards (int worth)
    47.     {
    48.         yards += worth;
    49.         if (yards <= 0)
    50.         {
    51.             yards = 0;
    52.         }
    53.     }
    54.  
    55.     public void Touchdown(int worth)
    56.     {
    57.         touchdowns += worth;
    58.         if (touchdowns <= 0)
    59.         {
    60.             touchdowns = 0;
    61.         }
    62.     }
    63.  
    64.     public void FieldGoal(int worth)
    65.     {
    66.         fieldgoals += worth;
    67.         if (fieldgoals <= 0)
    68.         {
    69.             fieldgoals = 0;
    70.         }
    71.     }
    72.  
    73.     public void Tackle(int worth)
    74.     {
    75.         tackles += worth;
    76.         if (tackles <= 0)
    77.         {
    78.             tackles = 0;
    79.         }
    80.     }
    81.  
    82.     public void FumbleRecovery(int worth)
    83.     {
    84.         fumblerecovery += worth;
    85.         if (fumblerecovery <= 0)
    86.         {
    87.             fumblerecovery = 0;
    88.         }
    89.     }
    90.  
    91.     public void Interception(int worth)
    92.     {
    93.         interceptions += worth;
    94.         if (interceptions <= 0)
    95.         {
    96.             interceptions = 0;
    97.         }
    98.     }
    99.  
    100.     public void TurnoverOnDowns(int worth)
    101.     {
    102.         turnoverondowns += worth;
    103.         if (turnoverondowns <= 0)
    104.         {
    105.             turnoverondowns = 0;
    106.         }
    107.     }
    108. }
     
  10. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Well, you should never do text setting in Update generally. You should only update gui when you need to (a value changes for example). But yes, if those are the same text objects, then your Update will continue to run and write over the values. Update runs once a frame which means every frame you are setting the text equal to those values.
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I was wondering about if that was the problem, when I read this earlier. You should try referencing that script from your first script, and calling a 'ResetValues' there, which sets everything to zero (the variables) and updates the text, too.

    Agreed on what @Brathnann said about not using the Update loop to set text :)
     
  12. unity_4r_hs_ak-C7uBg

    unity_4r_hs_ak-C7uBg

    Joined:
    Jan 24, 2018
    Posts:
    23
    Yea I was starting to assume that may be the issue. I just renamed the Update to UpdateValues, and called the method at the end of all of the methods below. Initially it works well. The values are set back to zero. But upon updating again, the values all go back to the values they were before the reset method was called. I even tried adding that when the text indie the box is 0 to set the interger to 0, but that seems to be having the same effect. I have a feeling it has something to do with the ordering of the resetting. I'm just having trouble figuring out what the ideal order is.
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Check my response. You want to set all of the integer values to zero as well.
     
  14. unity_4r_hs_ak-C7uBg

    unity_4r_hs_ak-C7uBg

    Joined:
    Jan 24, 2018
    Posts:
    23
    That's probably a better idea than what I'm trying to do. So from the save script, reference the button script, which has a resetvalues there, which I call to set the values back to 0?
     
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sure, I mean the method could just set all the integers to zero and then you could call the method you said you renamed above (which would then set all of the text values).
     
  16. unity_4r_hs_ak-C7uBg

    unity_4r_hs_ak-C7uBg

    Joined:
    Jan 24, 2018
    Posts:
    23
    That worked :D
    Thank both of you so much. I've been pulling my hair out the last couple of days trying to get it to work. I've been teaching myself C# and Unity over time and sometimes I run into stuff I just can't quite grasp. But thank you both for explaining it and helping me. It means a lot!
     
  17. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    No problem, glad you got it working.
     
  18. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem. :) Some of my best triumphs in programming were just about surviving things like that lol.
    Take care, have fun. =)