- Home /
How do I create a save scene feature though int value in c#
I am trying to make a Five Nights at Freddy's Remake in Xbox and I can't figure out any save game methods though integers like
public int night1 = 1;
public int night2 = 2;
public int night3 = 3;
public int night4 = 4;
public int night5 = 5;
and the nights are the scenes. What I want to happen is when the player beats the night it automatically saves so that the next time the player goes to the main menu it will load the next night! Please Help and Thanks
Answer by Glurth · Dec 31, 2016 at 12:11 AM
If you just want to save a few values to disk, I suggest you take a look at using PlayerPrefs,
https://docs.unity3d.com/ScriptReference/PlayerPrefs.SetInt.html
@Glurth @andycodes Thank you both you have been a huge help if I have any problems I will let you know!
Answer by andycodes · Dec 31, 2016 at 12:50 AM
Assuming that there isn't any other data to be stored here, you could use PlayerPrefs.SetInt to store the int value and PlayerPrefs.GetInt for the load level function.
If you're using Unity UI buttons for the main menu, in the OnClick() box, call the ResumeGame() function:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LoadLevel : MonoBehaviour {
public void ResumeGame(){
SceneManager.LoadScene (PlayerPrefs.GetInt ("Night", 1));
//"Night" is the identifier for which scene the player has made it to in the game
// 1 is the placeholder that will be set by default if the player has not yet played the game.
//this will load the scene by its organizational build key.
}
public void SaveProgress(int night){
PlayerPrefs.SetInt ("Night", night);
//in this function, feed in the int value of the night that the player will be going to after beating the current level so that it loads the next level when they resume later on.
}
}
Then, when the player completes a level, call the SaveProgress() function at the end of the level to save that value to disk.
if there's anything else i can do to help with it let me know!
Quick question, I am very new to int values and that code was an example that I showed so how would I make my int values work because I am not sure where I put them and how I put them?