Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Apr 20, 2020 at 05:59 PM by aminkhosravi007.
avatar image
0
Question by aminkhosravi007 · Apr 15, 2020 at 04:18 PM · unity 5playerprefssaveload

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");
                           }
             });
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

2 Replies

  • Sort: 
avatar image
0

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 ?

Comment
Add comment · 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
0

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);
 }

@lipisis

Comment
Add comment · Show 3 · 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 lipisis · Apr 15, 2020 at 07:10 PM 0
Share

$$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

avatar image aminkhosravi007 · Apr 15, 2020 at 07:41 PM 0
Share

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

avatar image lipisis · Apr 16, 2020 at 07:56 AM 0
Share

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

Answers Answers and Comments

227 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 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

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


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