- Home /
my text size changes didnt saved by slider
Hello I want my changes of text's size be saved even after go next page and came back to same page.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SliderText2 : MonoBehaviour
{
public Text text2;
float textvalue =45;
public Slider Slider;
void Start()
{
text2 = GetComponent<Text>();
//text2.fontSize = 45;
PlayerPrefs.GetFloat ("Slider");
}
// Update is called once per frame
public void textUpdate2(float slidervalue)
{
//text2.fontSize = slidervalue;
textvalue = slidervalue;
text2.fontSize = Mathf.RoundToInt(textvalue*100);
PlayerPrefs.Save ();
}
}
Answer by fafase · Apr 29, 2019 at 06:34 AM
You are not saving the new value. PlayerPrefs.Save is not meant to update the value.
void Start()
{
text2 = GetComponent<Text>();
// text2.fontSize = PlayerPrefs.GetFloat ("Slider", 45);
text2.fontSize = PlayerPrefs.GetInt ("Slider", 45);
}
public void textUpdate2(float slidervalue)
{
text2.fontSize = Mathf.RoundToInt(textvalue*100);
// PlayerPrefs.SetFloat("Slider", text2.fontSize);
PlayerPrefs.SetInt("Slider", text2.fontSize);
}
The start method tries to get the Slider value from prefs and if there is none then 45 is assigned. When you update the text, the value is also saved with the Slider key. Next time Slider key is accessed from prefs, it should return the value.
Oh I see..Tqs alot!! Btw why it said....
Assets/SliderText2.cs(18,33): error CS0266: Cannot implicitly convert type float' to
int'. An explicit conversion exists (are you missing a cast?)
I guess it is happening on the SetFloat, it should be SetInt since you are setting an integer.
I never know that Get also can can to GetInt! Tqs im so noob.. It's working now ! Tqsm !! :')))) <3