Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Save n Load n Show Collected Coins

Discussion in 'Scripting' started by KeepTrying, May 19, 2014.

  1. KeepTrying

    KeepTrying

    Joined:
    Feb 23, 2011
    Posts:
    119
    Hi community, any help to make that "ScoreManager" script work ?

    He is keeping track of what have been collected, but when i restart the level everything is lost.

    The game got a "Time Based Score" and a "Coin Based Score", the time based
    is working great.


    _________________ScoreManager.js


    Code (csharp):
    1. #pragma strict
    2. var medStarCount = 0;
    3. var advStarCount = 0;
    4. var simpStarCount = 0;
    5.  
    6. function Update () {
    7.     if ( medStarCount == 10 ) {
    8.         advStarCount += 1;
    9.         medStarCount = 0;
    10.     }
    11.        
    12.     if ( simpStarCount == 30 ) {
    13.         advStarCount += 1;
    14.         simpStarCount = 0;;
    15.     }
    16.  
    17. }
    18.  
    19. function SaveStars () {
    20.     Debug.Log("SaveStars()");
    21.     var overallScore : int = simpStarCount + (medStarCount * 5) + (advStarCount * 10);
    22.     if(PlayerPrefs.GetInt(Application.loadedLevelName + "simpStarCount") + (PlayerPrefs.GetInt(Application.loadedLevelName + "medStarCount") * 5)
    23.         + (PlayerPrefs.GetInt(Application.loadedLevelName + "advStarCount") * 10) < overallScore) {
    24.         PlayerPrefs.SetInt(Application.loadedLevelName + "simpStarCount", simpStarCount);
    25.         PlayerPrefs.SetInt(Application.loadedLevelName + "medStarCount", medStarCount);
    26.         PlayerPrefs.SetInt(Application.loadedLevelName + "advStarCount", advStarCount);
    27.     }
    28. }
    29.  
    30. function OnGUI () {
    31.     // Make a multiline text area that modifies stringToEdit.
    32.     var stringToEdit = GUI.TextArea (Rect (Screen.width - 210, 10, 200, 100), "Blue : " + simpStarCount + "\nRed : " + medStarCount + "\nGold : " + advStarCount, 200);
    33. }

    _________________ScoreSaver.js


    Code (csharp):
    1. function Awake(){
    2.  
    3. }
    4.  
    5. function Start () {
    6.  
    7. }
    8.  
    9. function Update () {
    10.  
    11. }

    _________________Scoring.js


    Code (csharp):
    1. #pragma strict
    2. var Score = 0;
    3. var ScoreSpeed = 1;
    4. var highScore = 0;
    5. var End = false;
    6. var StartGui = true;
    7. var newHighscore = 0;
    8. var Gamelevel : String;
    9. var MainMenulevel : String;
    10. var hasEnded : boolean;
    11.  
    12. private var playerScoring : ScoreManager;
    13.  
    14. function Start () {
    15.     highScore = PlayerPrefs.GetInt(Application.loadedLevelName + "myHighscore");
    16.     InvokeRepeating("addScore", 0, 0.8);
    17.     playerScoring = GameObject.Find("Z 2DPlayer").GetComponent(ScoreManager);
    18. }
    19.  
    20. function Update () {
    21. //  Score = Time.time * ScoreSpeed;
    22. if ( End == true  !hasEnded ) {
    23.     Ending();
    24.     hasEnded = true;
    25. }
    26.  
    27. }
    28.  
    29.  
    30.  
    31. function addScore () {
    32.     Score += ScoreSpeed;
    33.  
    34. }
    35.  
    36.  
    37.  
    38. function Ending () {
    39.     YouLost();
    40.  
    41. }
    42.  
    43.  
    44. function YouLost () {
    45.  
    46.     Debug.Log(PlayerPrefs.GetInt(Application.loadedLevelName + "myHighscore")+"/"+Score);
    47.     CancelInvoke();
    48. if ( PlayerPrefs.GetInt(Application.loadedLevelName + "myHighscore") > Score ) {
    49.     Save();
    50.     }else{
    51.     if(PlayerPrefs.GetInt(Application.loadedLevelName + "myHighscore") == 0){
    52.     Save();
    53.     }
    54. }
    55.     StartGui = true;
    56.  
    57.     playerScoring.SaveStars();
    58.  
    59. }
    60.  
    61. function OnGUI () {
    62.  
    63.     if ( StartGui == false ) {
    64.     var stringToEdit = GUI.TextArea (Rect (10, 10, 200, 100), "Score: " + Score , 200);
    65.     }
    66.  
    67.     if ( StartGui == true ) {
    68.     GUI.Box (Rect (10,10,200,40), "Time : " + Score + "| Best : " + highScore);
    69.  
    70.  
    71.  
    72.     if (GUI.Button (Rect (250,10,80,40), "Restart")) {
    73.         Application.LoadLevel (Application.loadedLevel);
    74.     }
    75.  
    76.     // Make the second button.
    77.     if (GUI.Button (Rect (250,60,80,40), "Main Menu")) {
    78.         Application.LoadLevel (MainMenulevel);
    79.     }
    80.     }
    81. }
    82.  
    83. function Save () {
    84.     Debug.Log("Save()");
    85.     PlayerPrefs.SetInt(Application.loadedLevelName + "myHighscore", Score);
    86.    
    87.     End = false;
    88. }

    _________________"Star.js" Maybe that script is not relevant

    Code (csharp):
    1. #pragma strict
    2. //var Star : Transform;
    3. //private var Player : ScoreManager;
    4. //var advancedStar = false;
    5. //
    6. //
    7. //function Start () {
    8. //  Player = GameObject.FindWithTag("Player").GetComponent("ScoreManager");
    9. //}
    10. //
    11. //function OnTriggerEnter (collisionInfo : Collider) {
    12. //  if(collisionInfo.transform.tag=="Player")
    13. //    {
    14. //  if ( advancedStar == true ) {
    15. //  Player.collectStars += 1;
    16. //  }
    17. //  if ( advancedStar == false ) {
    18. //  Player.starCount += 1;
    19. //  }
    20. //  Destroy(gameObject);
    21. //  }
    22. //}
    23. //
    24. //function Update () {
    25. //  Star.Rotate(Vector3.forward * Time.deltaTime * 20);
    26. //}
    27. //

    Many thanks!
     

    Attached Files: