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

saves scores only one time

Discussion in 'Scripting' started by Deleted User, Mar 13, 2018.

  1. Deleted User

    Deleted User

    Guest

    hi,
    i need only after games scene is completely done , gained scores to be added to total of user.
    but now at each time that user go back to menu scene ,amount of gained scores are added too.
    how can i say only add scores when level is completely done. and when user plays the same level one more time, they cannot get for the second time score from that level. thanks alot. that was actually two question.
    here is whre my score is calculated:

    Code (CSharp):
    1.     public void RequiredItems(int imageNum)
    2.     {
    3.         if (!CounterDownDone)
    4.             return;
    5.        
    6.         if(ShowTotalItems < 1)
    7.         {
    8.             GameWinPanel.SetActive (true);
    9.         }
    10.  
    11.         if(imageNum == currentIndex)
    12.         {
    13.             gameScores += 5;
    14.             PlayerPrefs.SetInt ("Gamescores",gameScores);
    15.             gamescorestext.text = ((int)gameScores).ToString ();
    16.             ShowTotalItems--;
    17.             Showtotalitems.text = ((int)ShowTotalItems).ToString ();
    18.  
    19.             buttons[currentIndex].GetComponent<Image>().enabled = false;
    20.  
    21.             if (currentIndex < questions.Length -1)
    22.             {
    23.                 question.text = questions[currentIndex + 1];
    24.                 currentIndex++;
    25.             }
    26.             else
    27.                 question.text = "You've found all";
    28.         }
    29.  
    30.         else if(gameScores > 2)
    31.         {
    32.             gameScores -= 2;
    33.             PlayerPrefs.SetInt ("Gamescores",gameScores);
    34.             gamescorestext.text = ((int)gameScores).ToString ();
    35.         }
    36.     }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    What does it mean for a scene to be "completely done" in your game? Is there some particular event that happens then?

    If so, you should update and save your scores there. Not when exiting the scene or when entering the menu scene.

    And if you need to do this only the first time the level is completed, then you'll simply have to store a flag for each level indicating if it's been completed before.
     
    Deleted User likes this.
  3. Deleted User

    Deleted User

    Guest

    so here in this
    1. else
    2. question.text = "You've found all";
    3. when all buttons are successfully clicked and founded level win panel will appear. the problem is there . i cut and pasted that setint... to else but also that didnt work.
    4. i need to say only when all 5 buttons are founded and clicked save the score... but if user go to menu scene before that ...if go out of game in middle of game..then dont give score. can you take a look at my code above?
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Yes, it looks like you're adding to the score on line 14, which if I understand correctly, runs every time you find a button. But you want to do it only when the last one is found.

    You were on the right track moving that code into the "else" clause. Be sure to include the proper curly braces so that the else clause really is the whole set of statements you're thinking of.
     
    Deleted User likes this.
  5. Deleted User

    Deleted User

    Guest

    @JoeStrout , i am new to programming. could you please only for this thread help me with code ? that will really help me to finish this hidden object style game. thanks.
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    I doubt that. If you're so new to programming you struggle to move code into an "else" clause, I don't think you'll be able to complete your game. You need to put it on the back burner for a few months and spend some time working carefully through some C# tutorials like these.

    But anyway, it should look something like this.

    Code (CSharp):
    1.     public void RequiredItems(int imageNum)  {
    2.         if (!CounterDownDone) return;
    3.      
    4.         if (ShowTotalItems < 1) {
    5.             GameWinPanel.SetActive (true);
    6.         }
    7.         if (imageNum == currentIndex) {
    8.             ShowTotalItems--;
    9.             Showtotalitems.text = ((int)ShowTotalItems).ToString ();
    10.             buttons[currentIndex].GetComponent<Image>().enabled = false;
    11.             if (currentIndex < questions.Length -1) {
    12.                 question.text = questions[currentIndex + 1];
    13.                 currentIndex++;
    14.             } else {
    15.                 question.text = "You've found all";
    16.                 gameScores += 5;
    17.                 PlayerPrefs.SetInt ("Gamescores",gameScores);
    18.                 gamescorestext.text = ((int)gameScores).ToString ();
    19.             }
    20.         } else if(gameScores > 2) {
    21.             gameScores -= 2;
    22.             PlayerPrefs.SetInt ("Gamescores",gameScores);
    23.             gamescorestext.text = ((int)gameScores).ToString ();
    24.         }
    25.     }
     
    Deleted User likes this.
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I just finished telling him the exact same thing in a PM. sigh.
    After what was undoubtedly the same advice for his goals, minus the code (this time) ;)
     
    Deleted User likes this.
  8. Deleted User

    Deleted User

    Guest

    next time i talk with you only c# wise mode ! good luck and thanks. :)
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ya, just trying to help you out so you're not always so stuck. :)