Search Unity

Reset Button Help

Discussion in 'Scripting' started by JacksonTheXtremeGamer, Feb 22, 2020.

  1. JacksonTheXtremeGamer

    JacksonTheXtremeGamer

    Joined:
    Jun 15, 2019
    Posts:
    108
    I have been working on a reset button that resets the progress of the game by using playerprefs, but every time I jump into the main menu after beating a level out of play-testing, it doesn't want to work. A person told me I need to use a return value. But how exactly? Here is the script I'm using:
    Code (CSharp):
    1. public class GoldText : MonoBehaviour
    2. {
    3.     public static bool TargetOneIsDestroyed;
    4.     public static bool TargetTwoIsDestroyed;
    5.     public static bool TargetThreeIsDestroyed;
    6.     public GameObject White1;
    7.     public GameObject Gold1;
    8.     public GameObject White2;
    9.     public GameObject Gold2;
    10.     public GameObject White3;
    11.     public GameObject Gold3;
    12.     int ProgressA;
    13.     int ProgressB;
    14.     int ProgressC;
    15.  
    16.     void Start()
    17.     {
    18.         data();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if(TargetOneIsDestroyed == true && ScoringSystem.theScore == 0)
    25.         {
    26.             White1.SetActive(false);
    27.             Gold1.SetActive(true);
    28.             PlayerPrefs.SetInt("ProgressA", 1);
    29.         }
    30.  
    31.         if (TargetTwoIsDestroyed == true && ScoringSystem.theScore == 0)
    32.         {
    33.             White2.SetActive(false);
    34.             Gold2.SetActive(true);
    35.             PlayerPrefs.SetInt("ProgressB", 1);
    36.         }
    37.  
    38.         if (TargetThreeIsDestroyed == true && ScoringSystem.theScore == 0)
    39.         {
    40.             White3.SetActive(false);
    41.             Gold3.SetActive(true);
    42.             PlayerPrefs.SetInt("ProgressC", 1);
    43.         }
    44.  
    45.         if(PlayerPrefs.GetInt("ProgressA") == 1)
    46.         {
    47.             White1.SetActive(false);
    48.             Gold1.SetActive(true);
    49.         }
    50.  
    51.         if (PlayerPrefs.GetInt("ProgressB") == 1)
    52.         {
    53.             White2.SetActive(false);
    54.             Gold2.SetActive(true);
    55.         }
    56.  
    57.         if (PlayerPrefs.GetInt("ProgressC") == 1)
    58.         {
    59.             White3.SetActive(false);
    60.             Gold3.SetActive(true);
    61.         }
    62.     }
    63.  
    64.     void data()
    65.     {
    66.         PlayerPrefs.GetInt("ProgressA", 1);
    67.         PlayerPrefs.GetInt("ProgressB", 1);
    68.         PlayerPrefs.GetInt("ProgressC", 1);
    69.     }
    70.  
    71.     public void Reset()
    72.     {
    73.         White1.SetActive(true);
    74.         Gold1.SetActive(false);
    75.         White2.SetActive(true);
    76.         Gold2.SetActive(false);
    77.         White3.SetActive(true);
    78.         Gold3.SetActive(false);
    79.         PlayerPrefs.DeleteKey("ProgressA");
    80.         PlayerPrefs.DeleteKey("ProgressA");
    81.         PlayerPrefs.DeleteKey("ProgressA");
    82.         return;
    83.     }
    84. }
    I need to try to make the game work smooth without any road blocks. I'm just down to the one however. Any help is appreciated.
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113
    you obviously forgot to set the three bools back to false (TargetOneIsDestroyed ... ).
    static values are alive as long as your application is alive.

    this is exactly how bugs are implemented.
    people take coding design too lightly.

    you should take into account that you're likely to make such a mistake in the course of development, and preemptively take action for that not to become a problem.

    sometimes a bug with side-effects such as this can creep very deep into your state structure.
    pay attention and address the design to include failsafes whenever you can help it.

    two typical sources of bugs are a) plain misremembering to do something and b) assuming that the system does something else. you hit the both of these at the same time.

    sorry if I come across as preaching, but I think you can benefit from this simple lesson:
    ALWAYS TEST YOUR ASSUMPTIONS

    [edit]
    oh and btw, this is exactly what you should watch for whenever you use statics.
    they are a phenomenal gate for memory leaks and such. don't use them if you're don't want such persistence issues. make a singleton object instead.

    let a reference to it simply die, it'll lose any bad state, and it's easier to pursue a failure in having it persist, than a future failure in corrupted states.
     
    Last edited: Feb 22, 2020
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113
    and you don't need a return there, that's just a complete troll.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Please don't make multiple threads for the same question. I already answered this in the other thread. Specifically I wrote:

    "In your second script, the calls inside the data() function do nothing with the integers they get. I think you want to assign them to your variables by the same name. That won't happen automatically, you have to assign the return value to the variable."

    Code (csharp):
    1. PlayerPrefs.GetInt("ProgressA", 1);
    This is one of the "calls" I am referring to. What you are saying is "get the integer stored in PlayerPrefs and do nothing with it." That seems suspiciously like you don't intend to do that.

    What you MOST LIKELY want to do, and what will NOT HAPPEN AUTOMATICALLY, is to ASSIGN the return value to a variable for you to use later. I don't know your coding intentions, but I suspect that you want to do this instead:

    Code (csharp):
    1. ProgressA = PlayerPrefs.GetInt("ProgressA", 1);
    and to do that for all three. That will LOAD what is in PlayerPrefs into those variables. Just because their name is the same, that does not happen automatically.

    As far as what else you're trying to do with reset, that is the part I don't understand. Please see my other post and from now on, please use one forum post per question, or else some of the mods here will start locking / deleting your stray posts.

    Further, I highly recommend you come up with a better name than
    data()
    for that function. How about
    LoadStoredPreferenceValues();
    ? See how much better that communicates to you and anyone looking at your code? It's an action and specifies what the action is.
     
  5. JacksonTheXtremeGamer

    JacksonTheXtremeGamer

    Joined:
    Jun 15, 2019
    Posts:
    108
    Sorry. I got frustrated, and a headache trying to figure this out. That's all. I appreciate the help. But the return value wasn't working, until orionsyndrome told me about the TargetOneIsDestroyed, etc. functions. Again, I'm sorry. It won't happen again. And again I appreciate the help. After I get this uploaded on Simmer, I'm gonna take a coffee break. Cause my head is running on fumes here. Programming with new functions and all.
     
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113
    yeah maybe we're a bit nervous here, the knowledge gap is too wide.
    take your time. try to digest a basic example or two.

    these things are pretty basic, you should really consider watching a tut from someone respectable and experimenting with the possibilities before you jump to rolling your own.
     
  7. JacksonTheXtremeGamer

    JacksonTheXtremeGamer

    Joined:
    Jun 15, 2019
    Posts:
    108
    orionsyndrome and Kurt-Dekker like this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    HA! Very nice! I love the sphere head dudes and their whirling blades... what a great enemy!!! Dude, you have nothing to be frustrated about, your game is awesome! Stay on target, you're doing it RIGHT. This is what gamedev is all about.

    I collected all five crystals on the first level, after figuring out which boxes I had to blow up. I think I blew myself up three times by being too close to the boxes when I shot them. DOH! One of the whirling blade guys got me a few times too.

    Now, if I may offer some constructive advice. I don't know if you're using source control yet, such as git... I highly recommend you look into that and get your entire project under source control. There are tutorials to help you set it up, and your project now clearly represents many hours of work, and you don't want that to get messed up by some mis-click or mis-drag. I use git for every project, separate repositories, and I push the repository up to github, which allows for free private hosting. You can also host it publicly if you want, and everybody can see and learn from your code.

    Go JacksonTXG!!!
     
  9. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113
    yep, whirling blade guys are awesome. I had a laugh.
     
    JacksonTheXtremeGamer likes this.
  10. JacksonTheXtremeGamer

    JacksonTheXtremeGamer

    Joined:
    Jun 15, 2019
    Posts:
    108
    Making the script for that one took me for a ride. Glad you guys like it.
     
    Kurt-Dekker and orionsyndrome like this.
  11. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113
    Hey it's pretty decent, keep it up. (And I've seen a lot.)