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
€93.70
List Price:  €131.19
You save:  €37.48
£80.05
List Price:  £112.07
You save:  £32.02
CA$137.71
List Price:  CA$192.80
You save:  CA$55.09
A$154.26
List Price:  A$215.98
You save:  A$61.71
S$136.44
List Price:  S$191.02
You save:  S$54.58
HK$782.28
List Price:  HK$1,095.23
You save:  HK$312.94
CHF 92.05
List Price:  CHF 128.88
You save:  CHF 36.82
NOK kr1,109.79
List Price:  NOK kr1,553.76
You save:  NOK kr443.96
DKK kr698.91
List Price:  DKK kr978.50
You save:  DKK kr279.59
NZ$169.74
List Price:  NZ$237.65
You save:  NZ$67.90
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳11,042.16
List Price:  ৳15,459.46
You save:  ৳4,417.30
₹8,347.41
List Price:  ₹11,686.70
You save:  ₹3,339.29
RM476.70
List Price:  RM667.40
You save:  RM190.70
₦132,499.74
List Price:  ₦185,504.94
You save:  ₦53,005.20
₨28,001.17
List Price:  ₨39,202.76
You save:  ₨11,201.58
฿3,714.62
List Price:  ฿5,200.62
You save:  ฿1,486
₺3,244.87
List Price:  ₺4,542.95
You save:  ₺1,298.08
B$519.51
List Price:  B$727.34
You save:  B$207.82
R1,869.05
List Price:  R2,616.75
You save:  R747.69
Лв183.29
List Price:  Лв256.61
You save:  Лв73.32
₩138,572.52
List Price:  ₩194,007.07
You save:  ₩55,434.55
₪373.46
List Price:  ₪522.86
You save:  ₪149.39
₱5,779.67
List Price:  ₱8,091.77
You save:  ₱2,312.09
¥15,790.32
List Price:  ¥22,107.08
You save:  ¥6,316.76
MX$1,711.23
List Price:  MX$2,395.79
You save:  MX$684.56
QR367.61
List Price:  QR514.66
You save:  QR147.05
P1,428.79
List Price:  P2,000.36
You save:  P571.57
KSh13,337.69
List Price:  KSh18,673.30
You save:  KSh5,335.61
E£4,785.86
List Price:  E£6,700.39
You save:  E£1,914.53
ብር5,734.01
List Price:  ብር8,027.84
You save:  ብር2,293.83
Kz83,445.65
List Price:  Kz116,827.25
You save:  Kz33,381.60
CLP$96,144.23
List Price:  CLP$134,605.76
You save:  CLP$38,461.53
CN¥724.02
List Price:  CN¥1,013.66
You save:  CN¥289.64
RD$5,888.55
List Price:  RD$8,244.21
You save:  RD$2,355.65
DA13,463.25
List Price:  DA18,849.09
You save:  DA5,385.84
FJ$228.52
List Price:  FJ$319.94
You save:  FJ$91.42
Q782
List Price:  Q1,094.84
You save:  Q312.83
GY$21,061.13
List Price:  GY$29,486.43
You save:  GY$8,425.29
ISK kr14,047.59
List Price:  ISK kr19,667.19
You save:  ISK kr5,619.60
DH1,017.53
List Price:  DH1,424.59
You save:  DH407.05
L1,775.81
List Price:  L2,486.21
You save:  L710.39
ден5,781.23
List Price:  ден8,093.95
You save:  ден2,312.72
MOP$805.87
List Price:  MOP$1,128.25
You save:  MOP$322.38
N$1,880.02
List Price:  N$2,632.11
You save:  N$752.08
C$3,703.54
List Price:  C$5,185.10
You save:  C$1,481.56
रु13,357.63
List Price:  रु18,701.22
You save:  रु5,343.58
S/376.73
List Price:  S/527.44
You save:  S/150.71
K388.53
List Price:  K543.96
You save:  K155.42
SAR375.01
List Price:  SAR525.03
You save:  SAR150.01
ZK2,694.57
List Price:  ZK3,772.51
You save:  ZK1,077.93
L466.29
List Price:  L652.82
You save:  L186.53
Kč2,357.53
List Price:  Kč3,300.64
You save:  Kč943.10
Ft36,614.73
List Price:  Ft51,262.09
You save:  Ft14,647.36
SEK kr1,101.06
List Price:  SEK kr1,541.53
You save:  SEK kr440.47
ARS$88,143.94
List Price:  ARS$123,405.05
You save:  ARS$35,261.10
Bs695.54
List Price:  Bs973.79
You save:  Bs278.24
COP$387,832.67
List Price:  COP$542,981.25
You save:  COP$155,148.58
₡51,172.97
List Price:  ₡71,644.21
You save:  ₡20,471.23
L2,486.27
List Price:  L3,480.88
You save:  L994.60
₲751,868.13
List Price:  ₲1,052,645.46
You save:  ₲300,777.33
$U3,857.19
List Price:  $U5,400.22
You save:  $U1,543.03
zł406.88
List Price:  zł569.65
You save:  zł162.77
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.