Saving and Loading

7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€95.85
List Price:  €134.19
You save:  €38.34
£79.54
List Price:  £111.35
You save:  £31.81
CA$143.73
List Price:  CA$201.23
You save:  CA$57.50
A$159.93
List Price:  A$223.91
You save:  A$63.97
S$135.54
List Price:  S$189.77
You save:  S$54.22
HK$777.83
List Price:  HK$1,089
You save:  HK$311.16
CHF 89.34
List Price:  CHF 125.09
You save:  CHF 35.74
NOK kr1,131.88
List Price:  NOK kr1,584.68
You save:  NOK kr452.80
DKK kr715.08
List Price:  DKK kr1,001.15
You save:  DKK kr286.06
NZ$176.75
List Price:  NZ$247.46
You save:  NZ$70.70
د.إ367.26
List Price:  د.إ514.18
You save:  د.إ146.92
৳11,945.33
List Price:  ৳16,723.94
You save:  ৳4,778.61
₹8,493.77
List Price:  ₹11,891.62
You save:  ₹3,397.85
RM450.75
List Price:  RM631.07
You save:  RM180.32
₦154,938.50
List Price:  ₦216,920.10
You save:  ₦61,981.60
₨27,824.38
List Price:  ₨38,955.25
You save:  ₨11,130.86
฿3,419.79
List Price:  ฿4,787.84
You save:  ฿1,368.05
₺3,519.01
List Price:  ₺4,926.76
You save:  ₺1,407.74
B$608.53
List Price:  B$851.97
You save:  B$243.44
R1,830.80
List Price:  R2,563.19
You save:  R732.39
Лв187.32
List Price:  Лв262.25
You save:  Лв74.93
₩144,627.53
List Price:  ₩202,484.33
You save:  ₩57,856.80
₪365.39
List Price:  ₪511.57
You save:  ₪146.17
₱5,882.91
List Price:  ₱8,236.31
You save:  ₱2,353.40
¥15,643.93
List Price:  ¥21,902.13
You save:  ¥6,258.20
MX$2,007.58
List Price:  MX$2,810.70
You save:  MX$803.11
QR364.41
List Price:  QR510.19
You save:  QR145.78
P1,381.63
List Price:  P1,934.33
You save:  P552.70
KSh12,923.70
List Price:  KSh18,093.70
You save:  KSh5,170
E£5,087.81
List Price:  E£7,123.14
You save:  E£2,035.32
ብር12,764.56
List Price:  ብር17,870.89
You save:  ብር5,106.33
Kz91,790.82
List Price:  Kz128,510.82
You save:  Kz36,720
CLP$98,905.10
List Price:  CLP$138,471.10
You save:  CLP$39,566
CN¥729.56
List Price:  CN¥1,021.42
You save:  CN¥291.85
RD$6,087.13
List Price:  RD$8,522.23
You save:  RD$2,435.09
DA13,485.25
List Price:  DA18,879.89
You save:  DA5,394.64
FJ$231.62
List Price:  FJ$324.28
You save:  FJ$92.66
Q770.23
List Price:  Q1,078.35
You save:  Q308.12
GY$20,913.67
List Price:  GY$29,279.97
You save:  GY$8,366.30
ISK kr13,911.60
List Price:  ISK kr19,476.80
You save:  ISK kr5,565.20
DH1,006.07
List Price:  DH1,408.53
You save:  DH402.46
L1,836.70
List Price:  L2,571.46
You save:  L734.75
ден5,900.06
List Price:  ден8,260.33
You save:  ден2,360.26
MOP$800.26
List Price:  MOP$1,120.39
You save:  MOP$320.13
N$1,840.36
List Price:  N$2,576.58
You save:  N$736.22
C$3,678.42
List Price:  C$5,149.94
You save:  C$1,471.51
रु13,597.49
List Price:  रु19,037.04
You save:  रु5,439.54
S/372.23
List Price:  S/521.13
You save:  S/148.90
K405.36
List Price:  K567.52
You save:  K162.16
SAR375.62
List Price:  SAR525.88
You save:  SAR150.26
ZK2,766.46
List Price:  ZK3,873.15
You save:  ZK1,106.69
L477.10
List Price:  L667.96
You save:  L190.86
Kč2,408.98
List Price:  Kč3,372.68
You save:  Kč963.69
Ft39,689.03
List Price:  Ft55,566.23
You save:  Ft15,877.20
SEK kr1,103.13
List Price:  SEK kr1,544.43
You save:  SEK kr441.30
ARS$102,160.40
List Price:  ARS$143,028.65
You save:  ARS$40,868.24
Bs690.76
List Price:  Bs967.10
You save:  Bs276.33
COP$435,928.80
List Price:  COP$610,317.76
You save:  COP$174,388.96
₡50,434.81
List Price:  ₡70,610.75
You save:  ₡20,175.94
L2,537.51
List Price:  L3,552.62
You save:  L1,015.10
₲779,455.88
List Price:  ₲1,091,269.42
You save:  ₲311,813.53
$U4,474.38
List Price:  $U6,264.31
You save:  $U1,789.93
zł408.66
List Price:  zł572.14
You save:  zł163.48
Already have an account? Log In

Transcript

Alrighty, so what we want to do now is we want to adjust our high score, which is what will appear at the when we die or when we have the chance to use unity ads. So the way we want to do that though, is we need to actually be able to save and load data. So this is actually a pretty exciting episode. So right now when we press play, and we press the play button, and we die, the score is hitting 500 or 458. This is not ideal, for obvious reasons. So if we open up our score script The first thing you'll notice, and you may not notice that it's the same on yours, but it's definitely like this on mine is we have two squiggly lines on the team pro and text mesh pro UI, you do UI.

I don't know why this is this isn't affecting our game at all. But my inkling is I think what's happening is there's an error inside of Visual Studio mashing messing with this because this this this is a new feature to unity. To be natively unity, and it's just an error inside of Visual Studio, and nothing to worry about as not actually affecting our game. So if you see this in your project, don't worry about it. But if you don't see it, also, don't worry about it, but it's all good. First thing we want to do is create a public.

And we will make it public int, highest score school, not high school. And we want to create a reference to the high school UI. So we can just copy this line of code and at high school. Awesome. And then it's pretty simple. All we want to do is say in update, we can say if score is greater than high school.

Then we can just save hospital data. So how do we actually save Donna? Well, it's really simple inside of unity, we can just use player prefs and player prefs really powerful and really simple and get The job done exactly for what we need. So we're going to be using play press. So to do to use play press we need to do is write play press dot set int. So we're setting an integer variable, open, open parentheses, open speech quotes, we're going to call this variable High School.

So it's accessing a variable called High School. And if it isn't, it's going to create a variable called High School. After the print, after speech quotes, we press make a comma and we want to just make it equal to score. Why do we make an equal to score? Well, whenever score is higher than high school, and that is a new high score. So instead of then changing the variable of high score to score, we can just cut out the middleman and crew seven integer high score equal to score because what if it is, if score is higher than high school?

Oh my gosh, love scores in a sentence. If score is greater than high school, then we can set the image integer to whatever score is That'll work perfectly fine. But what about actually getting the high school? Well, how does that actually work? Well, let's create a stop method. Void stops.

And then we can write something as simple as high school equals player prefs dot get int. And then it's just asking what integer we want to get what pipes you want to get, and we just want to write High School. And as long as these is exactly the same with the same capitalization, everything, it'll work just fine. Then all we need to do is write High School UI dot text equals high school.to string. But I've just realized we have a problem here. It may not be obvious at first, but let's get back over to unity and we'll see if we can discover the problem.

So if we go over to our player If we open up our canvas and open up our game over menu UI, and go over to player, we can actually scroll down. And we can see that we have an empty spot for high school UI where we can drag in our high school. So pressing play, and pressing play button. Scores 123 and die. Okay, as score zero, but we just got the highest score of three. Let's restart the game.

Press play again. Die. Oh, now it's displaying a high scores three, what's going on? So it's actually saving the data bit. It's not displaying it correctly if we get the hospital on that round, if that makes sense. So for example, it's Let's be a high score again.

Press play 1234 bam, it shows three. It's saved for that we're actually still displaying three or why is it That, let's go over to our code and have a look. Okay? When are we adjusting the hospital very variable. Okay, we're adjusting it here, here and here, the only one that's actually setting the hospital integer variable here, the only time high score is actually being changed is in start, which only happens once at the start when this script is called. So we need to actually change some of our code here.

So right down here, where it says player prefs, set into high score equals score, we could do that. But to fix the problem that we're having, now, a better thing to do would be to change the high school. So we say High School. Equals school. Cool. So if score is greater than high school will now high score equals score fantastic.

And then we want to set out integer and we could just leave it at the score, but let's use high score. Just for consistency sake, and let's go inside of here and say, player prefs dot delete all can press play quickly. Bam, our save data should be later. Now we can delete this line of code, go back, press play. We're running, bam should be at zero. That's exactly what we expected.

Press play and bam, it should equal three. That is working perfectly. So if we restart it now, and we press play, and we die here, bam, high scores three. Awesome. So I hope that makes sense. When we, when we need to save data, we're just setting the integer called hospital and we're equally into the hospital variable.

And when we want to load the data where Getting a high school variable. And we'll work our way accessing player prefs and we're getting an integer and we're getting the integer of high school and we're setting that integer. We're setting this integer high score to the player prefs integer of high school. How many times do I say score in this episode? Okay. Awesome.

So now we have a working saving and loading system and our high school menu is working perfectly. In the next episode, we'll work on adjusting the menu to have a little bit of pizzazz and that is animations.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.