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.
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
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
}
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. :<
Your answer
Follow this Question
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
Best way to handle multiple buttons? (C#) 3 Answers
Change UI image in a conversation 1 Answer
How To input text from a button? 1 Answer