Answer the question
In order to leave comments, you need to log in
Why doesn't saving/loading PlayerPrefs work?
I want the variable to be saved when the button is clicked (as well as when the game is exited) and when another button is clicked, the variable is loaded. Here are my scripts:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Save : MonoBehaviour
{
// Start is called before the first frame update
MainScene silic = new MainScene();
void Start()
{
silic.silicon = PlayerPrefs.GetInt("sil");
print("loadd");
}
void OnMouseDown()
{
PlayerPrefs.SetInt("sil", silic.silicon);
PlayerPrefs.Save();
print("save");
}
public void OnApplicationQuit()
{
PlayerPrefs.Save();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Load : MonoBehaviour
{
// Start is called before the first frame update
MainScene silic = new MainScene();
void OnMouseDown()
{
silic.silicon = PlayerPrefs.GetInt("sil");
print("loadd");
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MainScene : MonoBehaviour
{
public int silicon;
// Start is called before the first frame update
void Update()
{
}
}
Answer the question
In order to leave comments, you need to log in
4 like
MainScene silic
in one you loaded the value, from the other you show. And the unity yells at you that you don't need to nuke MonoBehaviour, but you ignore it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question