unity3d - Unity C# PlayerPrefs / Unable to get it working -


just learning unity/c# , having big problems saving playerprefs. need use them although never explained, , codes i've tried make work online sources have far failed me. please keep in mind still learning, , feedback appreciated! im trying save time level completed in (after has died on stage) , keep singular high score using playerpref set function. here code far:

using unityengine; using system.collections;  public class playerscript : monobehaviour {      public float currenthealth = 100;           // player hp     private float restartdelay = 5f;            // time wait before restarting level     public float restarttimer;                  // timer count restarting level     public float thrust = 100f;     public rigidbody rb;     private string textfieldstring;             // declaring text field string     public bool isfinished;                     // checking if game has finished or not      public static float besttime = playerprefs.getfloat ("highscore", 0);     float gametime;        // use initialization     void start () {         rb = getcomponent<rigidbody>();     }      // update called once per frame     void update () {         gametime += time.deltatime;         if(currenthealth <= 0) {             currenthealth = 0;             restarttimer += time.deltatime;              // .. if reaches restart delay...             if(restarttimer >= restartdelay)             {                 // .. reload loaded level.                 application.loadlevel(application.loadedlevel);             }         }         //when enemies cured, calls code          if(gameobject.findgameobjectswithtag("enemy").length==0) {             isfinished = true;             //application.loadlevel(application.loadedlevel);         }         if (isfinished == true) {              bool isnewhighscore = false;             if (gametime < besttime)                 {                 playerprefs.setfloat ("highscore", gametime);                 debug.log ("highscoreset");                 }                 besttime = playerprefs.getfloat("best time");         }     }          void oncollisionenter(collision collision) {                 // reduce health             if (collision.gameobject.tag == "enemy") {                         currenthealth -= collision.gameobject.getcomponent<zombiebotai>().zombiedamage;                         rb.addforce (transform.forward * thrust);                 }         }      //event running healing bottles     void ontriggerenter(collider other){         switch(other.gameobject.tag)         {             case "heal":             changehp(25);             break;         }         //destroys healing bottle after collision         destroy (other.gameobject);     }     //displays current hp on gui     void ongui(){         textfieldstring = gui.textfield (new rect (35, 500, 140, 30),"hp\t: " + currenthealth.tostring());         textfieldstring = gui.textfield (new rect (800, 35, 140, 30),"best time\t: " + playerprefs.getfloat ("highscore"));         if(gameobject.findgameobjectswithtag("enemy").length==0) {             textfieldstring = gui.textfield (new rect (600, 300, 230, 30), "you have won game in " + gametime + " seconds!");         }         if (currenthealth <= 0) {             textfieldstring = gui.textfield (new rect (600, 300, 230, 30), "you have been infected! game on !!");                 }         }     //hp cant go above 100     void changehp(float change){         currenthealth += change;         if (currenthealth > 100) {             currenthealth=100;                 }      }  } 

you should fix these lines:

if (isfinished == true) {          bool isnewhighscore = false;         if (gametime < besttime)             {             playerprefs.setfloat ("highscore", gametime);             debug.log ("highscoreset");             }             besttime = playerprefs.getfloat("best time");     } 

playerprefs doesn't work that, @ first should assign variable value. have done here:

playerprefs.setfloat ("highscore", gametime); 

then should call:

playerprefs.save(); 

to save variable, if want load variable should use same key saving with.

besttime = playerprefs.getfloat("highscore"); 

hope looking for.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -