c# - Make my counter score save highest score even after app closes -
so making rocket app, still development. wrote code counts score when collide objects. how make saves highest score , can display it? leave scene sets score 0 :( please thank you!
using unityengine; using unityengine.ui; using system.collections; public class points1 : monobehaviour { public text counttext; public text wintext; private rigidbody rb; private int count; void start() { rb = getcomponent<rigidbody>(); count = 0; setcounttext(); wintext.text = ""; } void ontriggerenter(collider other) { if (other.gameobject.comparetag("pickup")) { other.gameobject.setactive(false); count = count + 100; setcounttext(); } if (other.gameobject.comparetag("minus300")) { other.gameobject.setactive(false); count = count -300; setcounttext(); } } void setcounttext() { counttext.text = "score: " + count.tostring(); if (count >= 5000) { wintext.text = "good job!"; } } }
edit: tried code kindly provided, doing wrong? doesn't work... think might need gui element display highest score.
using unityengine; using unityengine.ui; using system.collections; public class points1 : monobehaviour { public text counttext; public text wintext; private rigidbody rb; private int count; void start() { rb = getcomponent<rigidbody>(); count = 0; setcounttext(); wintext.text = ""; } void ontriggerenter(collider other) { if (other.gameobject.comparetag("pickup")) { other.gameobject.setactive(false); count = count + 100; setcounttext(); } if (other.gameobject.comparetag("minus300")) { other.gameobject.setactive(false); count = count -300; setcounttext(); } } void setcounttext() { playerprefs.setint("score", count); playerprefs.save(); count = playerprefs.getint("score", 0); counttext.text = "score: " + count.tostring(); if (count >= 5000) { wintext.text = "good job!"; } } } //playerprefs.setint("score", count); //playerprefs.save(); //count = playerprefs.getint("score", 0);
what want playerprefs class. playerprefs allows store data between sessions of game.
you can save data calling playerprefs.set supported data type (int, float or string), , using corresponding playerprefs.get.
for example, if want save score, can so.
playerprefs.setint("score", count); playerprefs.save();
and
count = playerprefs.getint("score", 0);
Comments
Post a Comment