K
K
KotMilkMeoW2021-12-15 19:40:11
C++ / C#
KotMilkMeoW, 2021-12-15 19:40:11

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()
    {
        
    }
    

   
}

Nothing happens when you click on the buttons or when you restart. The variable is always reset.

Ps I'm making a game for android

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2021-12-15
@freeExec

4 like

But no, you should learn c #, at least a couple of the first chapters.
You have 3 different pieces MainScene silicin 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 question

Ask a Question

731 491 924 answers to any question