- Home /
Playerprefs to save unlocked button
Hello I have a script in which when You click on a button, If you have enough coin, that button must be unlocked and the wood that is on top of it and acts as kind of price tag disappear. I tried Playerprefs but it didn't work for me. here is my script in which (wood1) and (wood2) must be disappeared while click on buttons;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class MainMenu3 : MonoBehaviour {
public Button WhitePlane;
public Button BluePlane;
public GameObject wood1;
public GameObject wood2;
void Start () {
BluePlane.onClick.AddListener (() => {
character_number=1;
if (UIManager2.coin_score>=1) {
UIManager2.coin_score--;
SceneManager.LoadScene ("Menu2");
}
});
WhitePlane.onClick.AddListener (() => {
character_number=2;
if (UIManager2.coin_score>=2){
UIManager2.coin_score--;
UIManager2.coin_score--;
SceneManager.LoadScene ("Menu2");
}
});
Answer by lipisis · Apr 15, 2020 at 06:07 PM
Your question title mentions playerprefs but your code doesn't have one so I can't really say what you tried with playerprefs and what's the problem.
Also, what's the question then ? You said something doesn't work and I'm pretty sure it's not the code that you've posted doesn't work so what you expect us to do here ?
Answer by aminkhosravi007 · Apr 15, 2020 at 06:32 PM
Sorry! I want a brief Tutorial or even a clue for my problem! as I'm new to unity, I didn't post my code with Playerprefs because I tried Multiple ways but non of them worked. I forgot to Mention character_number variable.here is one of my scripts;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class MainMenu3 : MonoBehaviour {
public Button WhitePlane;
public Button BluePlane;
public static int character_number;
public GameObject wood1;
public GameObject wood2;
void Start () {
character_number = PlayerPrefs.GetInt ("Number");
BluePlane.onClick.AddListener (() => {
character_number=1;
if (UIManager2.coin_score>=1) {
UIManager2.coin_score--;
wood1.setactive (false);
SceneManager.LoadScene ("Menu2");
}
});
WhitePlane.onClick.AddListener (() => {
character_number=2;
if (UIManager2.coin_score>=2){
UIManager2.coin_score--;
UIManager2.coin_score--;
wood2.setactive(false);
SceneManager.LoadScene ("Menu2");
}
});
void Update () {
PlayerPrefs.SetInt ("Number", character_number);
}
$$anonymous$$ost likely it doesn't get saved as you actually save it in Update function but after Scene$$anonymous$$anager.LoadScene() is called, you won't really reach next Update() anymore because "loading is set to complete in the next rendered frame, calling Scene$$anonymous$$anager.LoadScene forces all previous AsyncOperations to complete"
Also you are writing to playerprefs every frame which is way overkill. Ins$$anonymous$$d of saving it every frame, why not to save it only when character_number variable changes.
So solution: move PlayerPrefs.SetInt () before you load other scene, after you set chracter_number=1 . This should solve it + it will optimise your game.
P.S. For some reason I can only comment and can't answer anymore
Thanks but it didn't work! maybe I need to use another kind of Playerprefs (string I mean). $$anonymous$$aybe this Playerprefs has nothing to do with buttons (WhitePlane and BluePlane). It works fine but when I restart the game, every thing returns to default and I can't open my locked character until collecting enough coins again. Sorry this is my first game and I'm working on it for a long period of time. @lipisis
I don't know if you have a code, but you should if you don't - since you only remove wood1 / wood2 when plane is clicked, obviously when you start your game, it will be in default state as I don't see any code that would disabled wood1 and wood2 based on character_number before game starts.
Have something like: character_number = PlayerPrefs.GetInt ("Number"); if(chracter_number == 1){ wood1.setactive (false); }
Similar with second wood
Follow this Question
Related Questions
save unlocked character by Playerprefs 3 Answers
Saving character selection 1 Answer
PlayerPrefs for FPS? 1 Answer
Coding help: How to use xml serialization 1 Answer
Save/Load Variable with playerprefs 1 Answer