- Home /
Problem with Playerprefs not saving
Hi fellow game developers, i've been developing a simple mobile game that has a vehicle selector and a weapon selector, i've created the Vehicle selector first and it worked perfectly, but when i did the weapon one, Playerprefs didn't bother to save the selected weapon.
I've used the same code but changing the obvious variables so it gets the weapon array, so idk why it doesn't work, maybe it's a simple mistake that i'm not seeing but i've been trying to solve this for 1 week and idk what should i do.
This is the "Saving" Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CharSelector : MonoBehaviour
{
[SerializeField]
private GameObject [] characterList;
private int charIndex;
[SerializeField]
private GameObject [] weaponList;
private int wepIndex;
public int Coins_To_Unlocked;
public GameObject Next_Level;
public int max_level;
public string num_level;
public static int thelevel;
public int t;
// Start is called before the first frame update
void Awake()
{
PlayerManager.numberOfCoins = 0;
thelevel = PlayerPrefs.GetInt("thelevel", thelevel);
}
void Update()
{
for (int o = 1; o < max_level; o++)
{
if (PlayerManager.numberOfCoins == Coins_To_Unlocked && Events.level == o)
{
Next_Level.SetActive(true);
}
}
}
public static void the_level(int t)
{
thelevel = t;
PlayerPrefs.SetInt("thelevel", thelevel);
}
public void Next()
{
PlayerPrefs.SetInt("CharacterSelected", charIndex);
PlayerPrefs.SetInt("WeaponSelected", wepIndex);
Events.NextLevel();
SceneManager.LoadScene("Nivel 1");
}
public void _level()
{
the_level(t);
SceneManager.LoadScene(num_level);
}
public void ChangeVehicle(int index)
{
for (int i = 0; i < characterList.Length; i++)
{
characterList[i].SetActive(false);
}
this.charIndex = index;
characterList[index].SetActive(true);
}
public void ChangeWeapon(int pistol)
{
for (int j = 0; j < weaponList.Length; j++)
{
weaponList[j].SetActive(false);
}
wepIndex = pistol;
weaponList[pistol].SetActive(true);
}
}
and those are both Loader Scripts, the Vehicle one:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharLoader : MonoBehaviour
{
[SerializeField]
private GameObject[] characterPref;
public GameObject jugador;
// Start is called before the first frame update
void Start()
{
LoadCharacter();
}
// Update is called once per frame
void Update()
{
}
private void LoadCharacter()
{
int charIndex = PlayerPrefs.GetInt("CharacterSelected");
if (charIndex < 0)
{
Instantiate(characterPref[0]);
}
else
{
Instantiate(characterPref[charIndex], jugador.transform);
}
}
}
and the Weapons one:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeapLoader : MonoBehaviour
{
[SerializeField]
private GameObject[] weaponPref;
public GameObject jugador;
void Start()
{
LoadWeapon();
}
private void LoadWeapon()
{
int wepIndex = PlayerPrefs.GetInt("WeaponSelected");
if (wepIndex < 0)
{
Instantiate(weaponPref[0]);
}
else
{
Instantiate(weaponPref[wepIndex], jugador.transform);
}
}
}
thanks for the help in advance.
Answer by kaushiknis · Mar 15 at 05:36 PM
@Kaudra Above code is working fine. I believe the issue are not in these files. Can you check if you are saving weaponIndex from anywhere else in the game.
there is no other object saving that variable, but after checking every object i found what was wrong, i forgot to add the int selector of the list to the button, no wonder why it was not working, now i feel super dumb to forget that simple thing, thank you very munch for your help, i don't think i would've discovered that if i wasn't checking every single object.
Your answer

Follow this Question
Related Questions
A node in a childnode? 1 Answer
Making a list selection variable 1 Answer
Select next object in Hierarchy? 0 Answers
Weapon selection system 2 Answers
How to make a combo box 4 Answers