- Home /
Text update with sliders not working correctly
I have 10 sliders that have text components attatched to them, they are supposed to display the slider values and save the values to playerprefs. This all works perfectly except that some of the text boxes will not update/display their text when the scene is played again. Half of the text boxes populate their text values with their saved value from playerprefs, the other half return null, even though they're value is being saved properly.
here is my code to save the values (attached to each slider):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveSliderValue : MonoBehaviour {
public Slider Slider;
public float valueofslider;
void Start()
{
valueofslider = PlayerPrefs.GetFloat(gameObject.name + "valueofslider");
Slider.value = valueofslider;
}
void Update()
{
valueofslider = Slider.value;
if (Input.GetKeyDown(KeyCode.S))
{
PlayerPrefs.SetFloat(gameObject.name + "valueofslider", valueofslider);
Debug.Log("save");
}
}
}
and to display the values (attached to each text component):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class showvalue : MonoBehaviour {
Text percentageText;
// Use this for initialization
void Start () {
percentageText = GetComponent<Text>();
Debug.Log(percentageText);
}
// Update is called once per frame
public void textUpdate (float value)
{
if (percentageText != null)
percentageText.text = value.ToString();
else
Debug.Log("Variable percentagetext is not set.");
}
}
and the error:
Variable percentagetext is not set.
UnityEngine.Debug:Log(Object)
showvalue:textUpdate(Single) (at Assets/showvalue.cs:24)
UnityEngine.UI.Slider:set_value(Single)
SaveSliderValue:Start() (at Assets/SaveSliderValue.cs:19)
Answer by Martin_Gonzalez · Mar 27, 2018 at 07:27 PM
I suppose that every slider gameObject is called different right? (And you didn't answer to your other post saying that you can save the 10 values -.-)
Also check that the gameObject which has showvalue script has a Text component and just saying, ins$$anonymous$$d of calling this in the Start, call it in the Awake method.
Whats the difference? Awake is called before Start. So if you set everything in the Awake and then you pass values in the Start, you will have everything cover.
If you are running 2 starts at the same time, sometime will win one, and another time will win the other.
void Awake () {
percentageText = GetComponent<Text>();
Debug.Log(percentageText);
}
Answer by corbinyo · Mar 27, 2018 at 07:33 PM
Yes, in fact that was the answer, to change Start to Awake in this script:
Text percentageText;
// Use this for initialization
void Awake () {
percentageText = GetComponent<Text>();
//percentageText = GameObject.Find("ThePlayer").GetComponent<SaveSliderValue>().valueofslider;
}
// Update is called once per frame
public void textUpdate (float value)
{
if (percentageText != null)
percentageText.text = value.ToString();
else
Debug.Log("Variable percentagetext is not set.");
}
}