unity3d - Set player score with playerprefs -
i need save players score display highscore , keep each time game either restarted or scene restarted. have following:
public class score : monobehaviour { public static int score = 0; static int highscore = 0; public text text; public static void addpoint(){ score++; if (score > highscore) { highscore = score; } } void start(){ text = getcomponent<text> (); score = 0; highscore = playerprefs.getint ("highscore", 0); } void ondestroy(){ playerprefs.setint ("highscore", highscore); } void update () { text.text = "score: " + score + "\nhigh score: " + highscore; } }
this causes score , highscore incremented each time hit trigger gain point. use trigger if hit object restart scene.
void oncollisionenter2d(collision2d col) { if (col.gameobject.tag == "enemy") { print ("you collided!"); collided = true; rb.gravityscale = 10; rb.drag = 0; invoke("startover", 1.0f); } }
once scene restarted both variables highscore , score being reset zero. guessing because game not saving highscore. how can save score?
add playerprefs.setint
before game restarts
void oncollisionenter2d(collision2d col) { if (col.gameobject.tag == "enemy") { print ("you collided!"); collided = true; rb.gravityscale = 10; rb.drag = 0; playerprefs.setint ("highscore", highscore); invoke("startover", 1.0f); } }
Comments
Post a Comment