Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by corbinyo · Mar 27, 2018 at 04:10 PM · textslidervalue

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)
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Martin_Gonzalez · Mar 27, 2018 at 07:30 PM 0
Share

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);
  
      }

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

79 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Better way of making a 4.6UI slider to return even values only 1 Answer

Can I show a numeric variable from the slider on the GUI? 1 Answer

C# access slidervalue 1 Answer

Slider.value won't change to INT 1 Answer

my text size changes didnt saved by slider 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges