Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kaudra · Mar 15 at 08:11 AM · listselection

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kaudra · Mar 15 at 08:44 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

136 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Level selection from main menu? 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges