• 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 /
  • Help Room /
avatar image
Question by Pixelvoice · Aug 22, 2015 at 08:59 PM · c#uiguiplayerprefssettings

C# Player Prefs & Toggle| Will not save settings

{UPDATED!} Hey guys, a little help here with a simple toggle:

The following script is suppose to change a material when toggle isON. The script works, in regards to changing the material, but it will not save the settings when I begin a new game. Does anyone know what I should do differently? The script is annotated, and the problem areas are specified for quick revision.

 using UnityEngine;
 using System.Collections;
 //Had to copy and paste this into my project folder from the unity packadge, 
 //not accessing for some reason...
 using UnityEngine.UI;
 
 
 public class swapMaterial : MonoBehaviour {
 
     //plug in material here
     public Material anyMat;
     //plug in default material here
     public Material defaultMat;
     //object that is effected by this script.
     public GameObject anyObject;
     //bool that decides if specified object changes material to gold
     public bool isGold;
     //this bool is simply here for comparative use.
     private bool sotrue = true;
     //A toggle that the user can switch on and off to select the gold material or not.
     public Toggle GoldenToggle;
 //The int that is being stored...
     private int midas;
 
 
     //public Component currentmaterial;
 
     void Awake(){
 
         //*****Help:Suppose to get the settings the player has chosen. Is it gold or not?
         midas = PlayerPrefs.GetInt("midas");
 
 //compares the stored int deciding whether material is changed or not...
         if (midas == 1) {
             isGold = true;
         } else {
             isGold = false;
         }
         
     }
     
 
     void Update(){
 
 
         //If bool isGold is true, change material to the material plugged into anyMat.
         if (isGold == sotrue) {
             anyObject.GetComponent<MeshRenderer> ().sharedMaterial = anyMat;
             midas = 1;
 
         } 
         else {
             //Else the material of this object is the selected default.
             anyObject.GetComponent<MeshRenderer> ().sharedMaterial = defaultMat;
             midas = 0;
 
 
 
         }
         //If the user has switched the toggle on, then the effected object's material becomes "anyMat"
         //in this case, Gold.
         if(GoldenToggle.isOn == true){
             isGold = true;
 
             //*****HELP: Suppose to save the toggle as on! not working...sets the int based on isGold in this case 1
             PlayerPrefs.SetInt("midas", midas);
 
 
 
         } //If the toggle is off, then the effected object's material becomes default.
         else {
             isGold = false;
             //*****HELP: Suppose to save the toggle as off! not working...sets the int based on isGold in this case 0
             PlayerPrefs.SetInt("midas", midas);
             
         }
 
         //Saves the player's selected settings. In this case, gold or no gold?
         //HELP:*****Does not apear to save the settings...problem in above script?
         PlayerPrefs.Save ();
 
     
 
     
 }
 
 }

This toggle is a setting to be applied to a new game, so the object in question is gold on new game, or not. I hope someone has a fresh perspective! Thank you all. Happy Deving!

For anyone needing help This script: Successfully changes a material. (scripted to be plunged-into the inspector) Successfully connects to a toggle.

Intended as a setting to be applied on New Game.

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by the_game_maker · Aug 22, 2015 at 09:48 PM

have you tryed changing it to PlayerPrefs.GetString() and PlayerPrefs.SetString() as it should return true or false

Comment

People who like this

0 Show 2 · 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 the_game_maker · Aug 22, 2015 at 09:54 PM 0
Share

and if you are using SetInt() and GetInt() isGold should look like this:

 isGold = PlayerPrefs.GetInt ("Name");
 if (isGold == 1){
     //Do Something that is equal to true
 }else{
     //Do something that is equal to false
 }

avatar image Pixelvoice · Aug 23, 2015 at 01:32 AM 0
Share

Thank you for the suggestion, after you had replied, I took both pieces of your advice. i still could not get this to work. I appreciate your feedback, I am still having issues. :<

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

25 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

Related Questions

How to detect a touch inside a GUI 0 Answers

How to make a restart button pop up after character has died? 2 Answers

World Space and Overlay Canvas 0 Answers

Best way to handle multiple buttons? (C#) 3 Answers

World Space and Overlay Canvas 0 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