Make a script of button which gets value of UI.Toggle and stores it in a file
I am a beginner in Unity. Today I created this scene:
No script is attached yet. Please make a script in C# which activates when the "Save" button is pressed, then get the ON/OFF Boolean value of check boxes, and then store them to a file. Next time when I open the program, it loads the ON/OFF states of each check box from that file. Thanks!
Answer by corn · Jan 10, 2016 at 05:41 PM
I'd be glad to help you, but you have to write your script by yourself if you want to learn anything.
For easily storing values, you can use PlayerPrefs.SetInt. To get the toggles' values, use Toggle.isOn. To get references to your toggles, the easiest way be would be to set them by drag and drop in the inspector, by either making them public or using the Serializefield attribute, like so :
public class Save : MonoBehaviour
{
[SerializeField]
Toggle JavaScriptToggle;
[SerializeField]
Toggle PHPToggle;
// etc...
}
Attach this script to your save button. Then in the editor, drag and drop your JavaScript toggle on the script's JavaScriptToggle field, etc. That way, in your script you can know if your toggle is on with JavaScriptToggle.isOn
, and use PlayerPrefs.SetInt to save it (0 for false, 1 for true). Then when the game starts, you get all those saved values with PlayerPrefs.GetInt, and assign them to the toggles using Toggle.isOn again.
Finally, just set the save button's onClick event to call a function of your Save script, say, SaveToggles(), in which you will use PlayerPrefs to store all those values. In order to be able to call this function with the button's onClick, your method has to be public. So what you need is :
public void SaveButtons()
{
PlayerPrefs.SetInt("JavaScript", JavaScriptToggle.isOn ? 1 : 0);
// etc...
}
In case you don't know what JavaScriptToggle.isOn ? 1 : 0
means, it is a ternary operator. It means : "if JavaScriptToggle is true, then return 1. Else return 0".
Try to understand all this, and you'll have no problem writing your script.
Answer by GhaliaHamouda · Jul 01, 2016 at 10:22 PM
Have you found a solution ?? .. I need it too much
Your answer
![](https://koobas.hobune.stream/wayback/20220612055717im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trying to move UI Elements but they are not working 1 Answer
Storing playerprefs as string in inputfield.text 1 Answer
Update and Get PlayerPrefs during runtime? 0 Answers
How to add colours to individual letters in an RPG text box scrolling 0 Answers
2D Object effect like Life is Strange? 0 Answers