Search Unity

Whats wrong with this code

Discussion in 'Scripting' started by MIST0, Jul 25, 2014.

  1. MIST0

    MIST0

    Joined:
    Apr 26, 2014
    Posts:
    57
    Trying to store a value as a playerpref then display it, but it doesn't seem to work. Thanks for any help.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Score : MonoBehaviour {
    5.     public GUIText countText;
    6.     public GUIText endText;
    7.     public int score;
    8.     public string highscore;
    9.  
    10.  
    11.     void Start()
    12.     {
    13.     endText.text = "";
    14.     countText.text = "Score";
    15.     highscore = score.ToString ();
    16.    
    17.     }
    18.  
    19.     void OnTriggerEnter(Collider other)
    20.     {
    21.                 if (other.gameObject.tag == "PickUp") {
    22.                         score = score + 1;
    23.                         SetCountText ();
    24.                 }
    25.  
    26.                 if (other.gameObject.name == "Pick Up2") {
    27.                         score = score + 4;
    28.                         SetCountText ();
    29.                 }
    30.  
    31.                 if (other.gameObject.name == "Pick Up3") {
    32.                         score = score + 9;
    33.                         SetCountText ();
    34.                 }
    35.  
    36.                 if (other.gameObject.name == "Pick Up4") {
    37.                         score = score + 19;
    38.                         SetCountText ();
    39.                 }
    40.  
    41.                 if (other.gameObject.tag == "Player") {
    42.                         endText.text = "Final Score " + score.ToString ();      
    43.                 }
    44.                
    45.                        
    46.         }
    47.  
    48.    
    49.     void SetCountText()
    50.     {
    51.         countText.text = "Score " + score.ToString();
    52.     }
    53.  
    54.     void start1()
    55.     {
    56.  
    57.     }
    58.  
    59.     void start2()
    60.     {
    61.  
    62.         highscore = PlayerPrefs.GetString("Highscore");
    63.     }
    64.  
    65.     void Gameover()
    66.     {
    67.  
    68.                 //if (highss > PlayerPrefs.GetInt ("highscore"))
    69.         {
    70.             //PlayerPrefs.SetString ("highscore", highss);
    71.         }
    72.  
    73.     }
    74.  
    75.  
    76.     void OnGUI ()
    77.         {
    78.                
    79.             {
    80.                 GUI.Label (new Rect(300, 0, 100, 50),"High Score " + highscore );
    81.             }
    82.        
    83.             {
    84.                 if (GUILayout.Button("Restart", GUILayout.Height(50), GUILayout.Width(100)))
    85.                        
    86.                 Application.LoadLevel (1);
    87.             }
    88.  
    89.            
    90.        
    91.         }
    92.  
    93. }
    94.  
    95.  
    96.  
    97.  
    98.  
    99.  
    100.  
     
  2. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    your start2() function is never called
     
    Arowx likes this.
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    it's not going to break anything, but a niggle would be that start1 and start2 are lower case, functions should really have an upper case first letter... and personally I'd call them something a little more meaning full: "LevelStart()" or whatever they're doing. It'll avoid confusion later on :)

    I'd also advise having the pickups store their score value rather than hardcoding it into the script picking them up... and for the first pickup you use tag, the rest are by name... if you have a "pickup" script which just holds their value you can do a getcomponent for "pickup" check if it is not null and work from there

    Code (csharp):
    1.  
    2. void OnTriggerEnter(Collider other)
    3. {
    4.     Pickup oPickup = other.GetComponent<Pickup>();
    5.     if (oPickup)
    6.     {
    7.             score += oPickup.value;
    8.             SetCountText ();
    9.     }
    10. }
    11.  
    (not tested so might have typos :) )

    mean you can just drop more pickups into your scene without changing the code in the player script
     
    Last edited: Jul 25, 2014
  4. MIST0

    MIST0

    Joined:
    Apr 26, 2014
    Posts:
    57
    I got the right value displaying in the highscore, but the score is not stored after the game is restarted or reset.
    Code (CSharp):
    1. if (other.gameObject.tag == "Player") {
    2.                 GameScore();
    3.             }
    4.                    
    5.         }
    6.  
    7.    
    8.     void SetCountText()
    9.     {
    10.         countText.text = "Score " + score.ToString();
    11.     }
    12.  
    13.     void GameScore()
    14.     {
    15.         highscore = score.ToString ();
    16.         highscore = PlayerPrefs.GetString ("Highscore", score.ToString());
    17.     }
     
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    SetString
    Save
     
  6. MIST0

    MIST0

    Joined:
    Apr 26, 2014
    Posts:
    57
    Ok thanks I am getting there lol, I am trying to add an if statement so the score is updated on if the score is higher than the saved one but I am getting for following error.

    Assets/Scripts/Score.cs(58,21): error CS0019: Operator `>' cannot be applied to operands of type `string' and `string'


    Code (CSharp):
    1. if (other.gameObject.tag == "Player") {
    2.                 GameScore();
    3.             }
    4.                    
    5.         }
    6.  
    7.    
    8.     void SetCountText()
    9.     {
    10.         countText.text = "Score " + score.ToString();
    11.     }
    12.  
    13.     void GameScore()
    14.     {
    15.         highscore = PlayerPrefs.GetString ("Highscore", score.ToString());
    16.         if (highscore > PlayerPrefs.GetString ("Highscore")){
    17.  
    18.         PlayerPrefs.SetString ("Highscore", score.ToString ());
    19.         PlayerPrefs.Save ();
    20.         }
    21.     }
    22.  
    23.     void OnGUI ()
    24.         {
    25.                
    26.             {
    27.                 GUI.Label (new Rect(300, 0, 100, 50),"High Score " + highscore );
    28.             }
     
  7. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    In line 16. you are trying to compare two strings like they are a float values.

    You maybe like to to save your Highscore as a float , or convert it back to float for comparison.
     
  8. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Yes you are not really getting the idea of set and get. Like above, line 16 area.
     
  9. MIST0

    MIST0

    Joined:
    Apr 26, 2014
    Posts:
    57
    I don't suppose you could give me more details or provide an example, how would I save my Highscore as a float?
     
  10. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Obviously, you are not an experienced coder.

    At Line 16, you get a value, then you compare some other value to... Not the value you just got, but instead, you get that value again. Makes no sense, get the value once, and store it, compare to the stored value.

    Further, when you initially get the value high score, you also add in a value. This is illogical,and I would imagine causes an error. You say get value, but pass a value. Its dumb. Sorry, but makes no sense.
     
  11. Dublinjonny

    Dublinjonny

    Joined:
    May 31, 2013
    Posts:
    61
    Im not entirely sure what you are trying to save and why you want to save the score and the high score , if you explained a little more I might be able to help better .

    Anyway just copy and paste this script I wrote for you and It should do the job , make whatever changes you need to and be sure to set the GUIStyle settings in the Inspector



    Code (CSharp):
    1. // Script for MiSTO
    2.  
    3.  
    4. // In the Inspector , Select the GUIStyles countText and End Text . From In the make sure they are alligned to middle Center and
    5. // chane the colour , font , size etc in there.
    6.  
    7. // Im not entirely sure why you want to save the game score and the Highscore to Prefs when normally a Highscore save should do ??
    8. // Anyway both are being saved below but in the start function the game score is being reset to ZERO , remove this if you need to.
    9.  
    10. using UnityEngine;
    11. using System.Collections;
    12.  
    13. public class Score : MonoBehaviour {
    14.  
    15.  
    16.     public GUIStyle  countText, endText;
    17.  
    18.     int              score,highScore;
    19.  
    20.     const string     _myScore = "myScore" , _myHighScore = "myHighScore" ;
    21.  
    22.     bool             displayEndScore;
    23.    
    24.     void Start(){
    25.  
    26.         score = PlayerPrefs.GetInt ("myScore", 0);
    27.         highScore = PlayerPrefs.GetInt ("myHighScore", 0);
    28.  
    29.         if (PlayerPrefs.GetInt ("myScore") >= 1){  //    This Will Reset the Level Score When the Level is Restarted (I`m Not sure why you need to save this ??)
    30.         PlayerPrefs.SetInt("myScore", 0);
    31.         }
    32.  
    33.         }
    34.    
    35.     void OnTriggerEnter(Collider other)
    36.     {
    37.         if (other.gameObject.tag == "PickUp") {
    38.             PlayerPrefs.SetInt ("myScore", PlayerPrefs.GetInt ("myScore") + 1);
    39.             PlayerPrefs.SetInt ("myHighScore", PlayerPrefs.GetInt ("myHighScore") + 1);
    40.         }
    41.        
    42.         if (other.gameObject.name == "Pick Up2") {
    43.             PlayerPrefs.SetInt ("myScore", PlayerPrefs.GetInt ("myScore") + 4);
    44.             PlayerPrefs.SetInt ("myHighScore", PlayerPrefs.GetInt ("myHighScore") + 4);
    45.         }
    46.        
    47.         if (other.gameObject.name == "Pick Up3") {
    48.             PlayerPrefs.SetInt ("myScore", PlayerPrefs.GetInt ("myScore") + 9);
    49.             PlayerPrefs.SetInt ("myHighScore", PlayerPrefs.GetInt ("myHighScore") + 9);
    50.         }
    51.        
    52.         if (other.gameObject.name == "Pick Up4") {
    53.             PlayerPrefs.SetInt ("myScore", PlayerPrefs.GetInt ("myScore") + 19);
    54.             PlayerPrefs.SetInt ("myHighScore", PlayerPrefs.GetInt ("myHighScore") + 19);
    55.         }
    56.        
    57.         if (other.gameObject.tag == "Player") {
    58.         displayEndScore = true;   // This Bool will Put you in End Of Game Mode
    59.        
    60.         }
    61.         else{
    62.         displayEndScore = false;
    63.         }
    64. }
    65.    
    66.  
    67.     IEnumerator endGameScore (){
    68.  
    69.         yield return new WaitForSeconds (5f);// This will display your Highscore on the screen for 5 seconds before loading
    70.         PlayerPrefs.Save ();
    71.         Application.LoadLevel (1); // This will load you back to the Main Menu / previous screen after the game ends ** Set your own level.
    72.         yield return null;
    73.     }
    74.    
    75.  
    76.     void OnGUI (){
    77.  
    78.     GUI.Label (new Rect(Screen.width / 3f, Screen.height / 5.0f, Screen.width / 3.5f, Screen.height / 10f), "Score:" +_myScore ,countText);
    79.  
    80.       if (GUI.Button (new Rect (Screen.width / 2.75f, Screen.height / 6f, Screen.width / 4f, Screen.height /5f),  "")){
    81.        
    82.          PlayerPrefs.Save();
    83.          Application.LoadLevel (1);
    84.         }
    85.        
    86.         if (displayEndScore == true){
    87.         GUI.Label (new Rect(Screen.width / 3f, Screen.height / 5.0f, Screen.width / 3.5f, Screen.height / 10f), "HighScore:" +_myHighScore ,endText);
    88.         Time.timeScale = 0;
    89.         StartCoroutine (endGameScore ());
    90.         }
    91.         else {
    92.         Time.timeScale = 1;  
    93.         }
    94.  
    95.     }
    96. }
    97.  
    98.  
    99.  
    100.  
    101.  
    102.  
    103.  
    104.  
     
    Last edited: Jul 29, 2014
    MIST0 likes this.
  12. MIST0

    MIST0

    Joined:
    Apr 26, 2014
    Posts:
    57
    Thanks very much for your help, and yes I don't really know what I am doing lol, in reply to your comment

    if (PlayerPrefs.GetInt ("myScore") >= 1) (I`m Not sure why you need to save this ??)"

    All I am trying to do is save and display the Highest score when the game finishes and during the game, and just update it when a higher score is made.
     
  13. Dublinjonny

    Dublinjonny

    Joined:
    May 31, 2013
    Posts:
    61
    ok I understand now . In the script I wrote above all you need to do is compare the two values and if the high score is less Tha current score update the high score . Its easy enough to do . I'm away from my computer for most of the day . But if you wait until this evening / tomorrow ill rewrite that script to do exactly what your looking for
     
  14. MIST0

    MIST0

    Joined:
    Apr 26, 2014
    Posts:
    57
    Thanks very much, I have been working on mine, this is my code so far, its getting there, but it just adds the scores together in the high score.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Score : MonoBehaviour {
    5.     public GUIText countText;
    6.     public GUIText endText;
    7.     public int score;
    8.     public int highscore;
    9.  
    10.  
    11.     void Start()
    12.     {
    13.     endText.text = "";
    14.     countText.text = "Score";
    15.     highscore = PlayerPrefs.GetInt ("Highscore", 0);
    16.  
    17.        
    18.  
    19.  
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.  
    25.     }
    26.  
    27.     void OnTriggerEnter(Collider other)
    28.     {
    29.                 if (other.gameObject.tag == "PickUp") {
    30.                         score = score + 1;
    31.             PlayerPrefs.SetInt ("Highscore", PlayerPrefs.GetInt ("Highscore") + 1);
    32.                         SetCountText ();
    33.                 }
    34.  
    35.                 if (other.gameObject.name == "Pick Up2") {
    36.                         score = score + 4;
    37.             PlayerPrefs.SetInt ("Highscore", PlayerPrefs.GetInt ("Highscore") + 4);  
    38.                         SetCountText ();
    39.                 }
    40.  
    41.                 if (other.gameObject.name == "Pick Up3") {
    42.                         score = score + 9;
    43.             PlayerPrefs.SetInt ("Highscore", PlayerPrefs.GetInt ("Highscore") + 9);  
    44.                         SetCountText ();
    45.                 }
    46.  
    47.                 if (other.gameObject.name == "Pick Up4") {
    48.                         score = score + 19;
    49.             PlayerPrefs.SetInt ("Highscore", PlayerPrefs.GetInt ("Highscore") + 19);          
    50.                         SetCountText ();
    51.                 }
    52.  
    53.                 if (other.gameObject.tag == "Player") {
    54.                         endText.text = "Final Score " + score;      
    55.                 }
    56.                
    57.                    
    58.         }
    59.  
    60.    
    61.     void SetCountText()
    62.     {
    63.         countText.text = "Score " + score;
    64.     }
    65.  
    66.    
    67.     void OnGUI ()
    68.         {
    69.                
    70.             {
    71.                 GUI.Label (new Rect(300, 0, 100, 50),"High Score " + highscore );
    72.             }
    73.        
    74.             {
    75.                 if (GUILayout.Button("Restart", GUILayout.Height(50), GUILayout.Width(100)))
    76.                        
    77.                 Application.LoadLevel (1);
    78.             }
    79.  
    80.            
    81.        
    82.         }
    83.  
    84. }
    85.  
    86.  
    87.  
    88.  
    89.  
    90.  
    91.  
     
  15. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    on the first play through, say you score 100 points. This is stored in "highscore" in playerprefs.
    on the second play through that code picks up the "highscore" from playerprefs and then you start adding to it... say you collect a pickup1, score will do 0+1 = 1, highscore will do 100+1 = 101...

    these lines:
    Code (csharp):
    1.  
    2. PlayerPrefs.SetInt("Highscore", PlayerPrefs.GetInt("Highscore")+##);
    3.  
    need to be in an if statement to ensure the highscore is only updated when the current score is bigger than the currently held high score in playerprefs.

    i.e. something like
    Code (csharp):
    1.  
    2. if(score > PlayerPrefs.GetInt("Highscore"))
    3. {
    4. PlayerPrefs.SetInt("Highscore", score);
    5. }
    6.  
     
    MIST0 likes this.
  16. Dublinjonny

    Dublinjonny

    Joined:
    May 31, 2013
    Posts:
    61
    O.k this Updated script will do the following if you just copy and paste it

    1. It will display the score during the level as its played but not save the score of the level as it is not needed for example you play the game once and get 40 points it will not save that 40 points so when you play the level again the score will be set to zero

    2. it will display the highest score that you have got on the level so if for example the highest sore you achieved was the previous 40 points from a previous game then 40 points will be displayed

    3. it will update the highscore as it increases . Example the highscore is 40 points and you are playing the game and during the game you manage to beat your high score of 40 points and hit 41 the 42 , 43 etc etc the highscore will live update in the display to match the new highscore and of course save it as the new high score

    4. the scoremaster feature will add all the scores you have achieved , so if in game one you get 40 points it will store 40 points in game two you get 50 points it will store 90 points and so on , I added this just in case you wanted to keep the total points achieved for ranking player/ leveling players up etc etc .

    5. at the end of the game you will not be able to display the score unless you add an IEnumerator and delay the scene moving back to the main menu etc .

    So copy and paste it and try it out

    Code (CSharp):
    1. // Script for MiSTO
    2.  
    3.  
    4. // In the Inspector , Select the GUIStyles countText and End Text . From In the make sure they are alligned to middle Center and
    5. // chane the colour , font , size etc in there.
    6.  
    7. // Im not entirely sure why you want to save the game score and the Highscore to Prefs when normally a Highscore save should do ??
    8. // Anyway both are being saved below but in the start function the game score is being reset to ZERO , remove this if you need to.
    9.  
    10. using UnityEngine;
    11. using System.Collections;
    12.  
    13. public class Score : MonoBehaviour {
    14.  
    15.  
    16.     public GUIStyle   countText, endText;
    17.  
    18.     int                score;
    19.  
    20.     public static int  highScore, scoreMaster;
    21.  
    22.     const string       _myHighScore = "myHighScore" , _myScoreMaster = "myScoreMaster";
    23.  
    24.     bool              displayEndScore;
    25.  
    26.     void Start(){
    27.  
    28.  
    29.         scoreMaster   = PlayerPrefs.GetInt("myScoreMaster",0);
    30.  
    31.         highScore     = PlayerPrefs.GetInt ("myHighScore", 0);
    32.    
    33.  
    34.    
    35.     }
    36.  
    37.     void Update(){
    38.  
    39.         if (  PlayerPrefs.GetInt ("myHighScore", 0) < score) {
    40.             PlayerPrefs.SetInt ("myHighScore", score);
    41.         }
    42.  
    43.         }
    44.  
    45.     void OnTriggerEnter(Collider other)
    46.     {
    47.         if (other.gameObject.tag == "PickUp") {
    48.             score += 1;
    49.             PlayerPrefs.SetInt ("myScoreMaster", PlayerPrefs.GetInt ("myScoreMaster") + 1);
    50.         }
    51.    
    52.         if (other.gameObject.name == "Pick Up2") {
    53.             score += 4;
    54.             PlayerPrefs.SetInt ("myScoreMaster", PlayerPrefs.GetInt ("myScoreMaster") + 4);
    55.         }
    56.    
    57.         if (other.gameObject.name == "Pick Up3") {
    58.             score += 9;
    59.             PlayerPrefs.SetInt ("myScoreMaster", PlayerPrefs.GetInt ("myScoreMaster") + 9);
    60.         }
    61.    
    62.         if (other.gameObject.name == "Pick Up4") {
    63.             score += 19;
    64.             PlayerPrefs.SetInt ("myScoreMaster", PlayerPrefs.GetInt ("myScoreMaster") + 19);
    65.         }
    66.    
    67.         if (other.gameObject.tag == "Player") {
    68.             displayEndScore = true;   // This Bool will Put you in End Of Game Mode
    69.        
    70.         }
    71.         else{
    72.             displayEndScore = false;
    73.         }
    74.     }
    75.  
    76.  
    77.     IEnumerator endGameScore (){
    78.    
    79.         yield return new WaitForSeconds (5f);// This will display your Highscore on the screen for 5 seconds before loading
    80.         PlayerPrefs.Save ();
    81.         Application.LoadLevel (1); // This will load you back to the Main Menu / previous screen after the game ends ** Set your own level.
    82.         yield return null;
    83.     }
    84.  
    85.  
    86.     void OnGUI (){
    87.         GUI.Label (new Rect(Screen.width / 3f, Screen.height / 5.0f, Screen.width / 3.5f, Screen.height / 10f), "HighScore:" +_myHighScore ,countText);
    88.         GUI.Label (new Rect(Screen.width / 3f, Screen.height / 5.0f, Screen.width / 3.5f, Screen.height / 10f), "Score:" +score ,countText);
    89.         GUI.Label (new Rect(Screen.width / 1.95f, Screen.height / 2.23f, Screen.width / 3.5f, Screen.height / 10f), "Total Score" +scoreMaster,countText);
    90.    
    91.         if (GUI.Button (new Rect (Screen.width / 2.75f, Screen.height / 6f, Screen.width / 4f, Screen.height /5f),  "")){
    92.        
    93.             PlayerPrefs.Save();
    94.             Application.LoadLevel (1);
    95.         }
    96.    
    97.         if (displayEndScore == true){
    98.             GUI.Label (new Rect(Screen.width / 1.95f, Screen.height / 2.23f, Screen.width / 3.5f, Screen.height / 10f), "Total Score" +scoreMaster,endText);
    99.             GUI.Label (new Rect(Screen.width / 3f, Screen.height / 5.0f, Screen.width / 3.5f, Screen.height / 10f), "HighScore:" +_myHighScore ,endText);
    100.             GUI.Label (new Rect(Screen.width / 3f, Screen.height / 5.0f, Screen.width / 3.5f, Screen.height / 10f), "Score:" +score ,endText);
    101.             Time.timeScale = 0;
    102.             StartCoroutine (endGameScore ());
    103.         }
    104.         else {
    105.             Time.timeScale = 1;
    106.         }
    107.    
    108.     }
    109. }
    110.  
    111.  
    112.  
    113.  
    114.  
    115.  
    116.  
    117.  
    118.  
    119.  
     
    Last edited: Jul 31, 2014
    MIST0 likes this.
  17. Dublinjonny

    Dublinjonny

    Joined:
    May 31, 2013
    Posts:
    61
    oh Snap Lefty lol , exactly what Ive got above lol
     
  18. MIST0

    MIST0

    Joined:
    Apr 26, 2014
    Posts:
    57
    Great thank you, its working just the way I want, it took long enough lol