I need some help with this code.. I'm new to this and I'm trying to learn by making my 100% own game

How do i Load my previous scene?

I have 4 scenes. MainMenu, DeathScreen, lvl 1 and lvl 2. When I die in lvl 2 for example, I want to press the restart button in the DeathScreen but that only takes me to the first lvl. because in the code it says only to load one lvl. this means that i have to make different DeathScreens for every lvl…
I want to be able to load the last lvl from the DeathScreen. how do i achieve this? help pls :slight_smile:

Like a large percentage of games, you’ll have certain stats (variables) that you want to keep track of while playing, regardless of which scene you’re on.
If you’re looking to learn at making an entire game, as you said… Look into persisting data between scenes. There are several options available to you, and you will simply need an easy one (of any of them) to do this, I think.

Storing the scene index you’re on (if it’s lvl1 or lvl2 – probably don’t care about menu/death scene for this purpose)
When you die, you can reload by index (or by name is an option, too, of course).

For learning, I think this will help you not just in this situation, but also as you expand the game, too. :slight_smile:

1 Like

TY. can u give an example? like how u would do it? i would love that. i have not really found “the perfect tutorial” so an example would be amazing! been trying to learn in by myself during this last month :slight_smile:

And also if you dont mind. i have a discord server that u could join if u wanna. I would love to get some help thro that as well :slight_smile: https://discord.gg/33AQs (discord server address)

I don’t use discord, but I think you’d benefit from looking into this part.
Here are some solutions.

A static variable in a class.
using ‘PlayerPrefs’
a script using DontDestroyOnLoad

Those are 3 options for you to look into for simple ways to persist data/access variables as your game changes scenes, etc…
The situation will sometimes prefer one over another, and sometimes it’s just up to you to choose which one you’d like, as any option would suffice.
I hope that makes sense. I hope this helps you more than just offering you 1 example which may work, but may not give you as much insight as looking into it :slight_smile:

Start with those 3. Just read a few pages. I’m confident you’ll see some sense in it.

thanks mate i will get right into it!

No problem :slight_smile:
After you look into it, feel free to write back if you have any issues/questions… :slight_smile:

1 Like

Okey so this is how far i have gotten with the PlayerPrefs thingy.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour
{
public Text Text;
public int score;

void Awake()
{
score = 0;
Text.text = "score: " + score.ToString ();
}

void OnTriggerEnter(Collider other)
{
if(other.CompareTag(“Coin”))
{
score += 1;
Text.text = "score: " + score.ToString ();
}
}

void Start()
{

PlayerPrefs.SetInt(“score”, score);

PlayerPrefs.GetString(“score”).ToString();
}
}

for some reason it does not work… i might have missed a step or two but i cant tell wich ones ;D

The score counter works as it should but the saving part does not.

Cool. That’s a start. I thought you wanted to load the proper scene, but I get how storing coins is also good, too.

You mixed up the types when you’re Getting the “score” variable. You want to retrieve an ‘int’.

int score = PlayerPrefs.GetInt("score",0);  // the '0' is the default value if it doesn't exist.

You probably don’t need to ‘SetInt’ right away in Start, since you want the value from GetInt instead, because you’re loading (whether it’s a level or the game has just opened and you’re loading a previous play session’s score… whatever).

:slight_smile:
Just remember to SetInt for the score when your score increases. Technically you could do it anytime before you change scenes or whatever… So long as you’re not retrieving it, again, in between… the ‘score’ variable would still work. Hope that makes sense. Good luck with your progression of things

im really sorry i might sound like an idiot ;D but i really cant make sence of this… are you sure you cant join like skype or something? im really bad at understanding stuff whrn it is in writing. i have ADHD and dyslexia sooo… :slight_smile: skype would prob be the best considering that I can share screen.

Sorry, I try to keep my communications on the forums…
This is a simple example for loading/saving a score:

int score;
void Awake() {
   score = PlayerPrefs.GetInt("score", 0);
   }
void AddScore(int num) {
   score += num;
   PlayerPrefs.SetInt("score", score);
   }
// you can periodically call "PlayerPrefs.Save()" to force changes made be written to disk. Changes are usually updated automatically to disk when the app closes. Refer to the documentation for the exact specifications.

What part(s) weren’t you making sense of? Try to explain: What you have, and what’s working and/or not working, and what you’d like to see happen.
I can’t know what you mean if you just say it doesn’t make sense generally.

okey i see :slight_smile: i will try to explain. so i think i have just come up with a way to save some kind of highscore. but i would like the score to follow to lvl 2 from lvl 1 and so on.

Okay, well that’s no problem. PlayerPrefs can do that.
So, if you have the loading part, in my example where it assigns score from PlayerPrefs
then when you gain in score, you are adjusting your score, etc…
When you load the next level, just repeat the same idea. A script in that level looks up the key “score” with int score = PlayerPrefs.GetInt(“score”, 0);
and you continue the pattern.

*If you try this, and it happens to not work, try adding ‘PlayerPrefs.Save()’ just before you change scenes.

After you’ve done these steps… Write back and say how it turned out :slight_smile:

okey what do you mean by “adding PlayerPrefs.Save() right before i change scenes”? like do you mean right before the game executes the change scene code or what?

Yep, but I only wrote that in case it didn’t work without it. I believe it will work without that, though.

okey :slight_smile: