0

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)

pictures - for understanding

after adjusting in playmode

sliders after entering playmode again

and if I remove the debug.log I get a null reference.

enter image description here

LarsTech
  • 80,625
  • 14
  • 153
  • 225
cyo
  • 69
  • 1
  • 14
  • It's not an error, it's a Debug Log... Check during execution that all your scripts have the Text attached. – Chopi Mar 27 '18 at 16:19

2 Answers2

2

The order of execution for Start functions can't be relied upon. Rename the showvalue Start function to Awake and see if it helps.

Basically what happens is:

  • some of showvalue instances execute their Start function earlier than their corresponding SaveSliderValue
  • thus they set the value of the text correctly
  • and for some that order is broken (because Start functions execute in arbitrary order) => your error

Awake is always executed before Start - use that to your advantage.

pdeschain
  • 1,411
  • 12
  • 21
  • Thank you, this worked! I will keep this method in mind when writing new functions such as these. Thank you @mr.pd – cyo Mar 27 '18 at 17:41
0

working code for the ShowValues script here:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;



public class showvalue : MonoBehaviour {


    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.");


    }
}
cyo
  • 69
  • 1
  • 14