Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Khorkhe · Sep 20, 2014 at 03:00 AM · uisliderunity 4.6

Unity 4.6 UI - set slider value via script

Hi,

I am trying to restore the user's preferences on a mobile game by reading previously saved values on PlayerPrefs (let's call the variable V). When the user goes to the Settings menu, I would like to set the volume slider to V. - I know V is in the range (0, 1).

I am attempting the following, but the value is not changing, as if the line was ignored:

 GameObject.Find ("sliderSensitivity").GetComponent<Slider>().value = PlayerPrefs.GetFloat("sensitivity");

Any clue? Thanks a lot

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Zephrim · Sep 20, 2014 at 04:31 AM

I took a few liberties with your code, but I think it should be a decent fit for what you're trying to do. I recommend you make sure that nothing returns null and erroring if it does.

Also the fact that you cannot find a GameObject if it isn't active in the scene has also messed with me a few times. So if it's the type of object that I plan on hiding quite often, then I'll manually look for it once and store it in an array or dictionary depending on what it is.

The Code

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class OptionsMenu : MonoBehaviour 
 {
     /// <summary>
     /// The Default Volume Level.
     /// </summary>
     /// <remarks>
     /// This will only get set if  the VolumeLevel 
     /// hasn't been previously saved.
     /// </remarks>
     public const float DefaultVolumeLevel = 0.5f;
 
     /// <summary>
     /// The Volume's Slider
     /// </summary>
     /// <remarks>
     /// Since we plan on using this multiple times, it's better to store it rather then continuously trying to find the component.
     /// </remarks>
     Slider volumeSlider;
 
     // Use this for initialization
     void Start() 
     {
         // Try to find the desired GameObject.
         // This will only find active GameObjects in the scene.
         GameObject temp = GameObject.Find("Volume Slider");
         if (temp != null)
         {
             // Get the Slider Component
             volumeSlider = temp.GetComponent<Slider>();
 
             // If a Slider Component was found on the GameObject.
             if (volumeSlider != null)
             {
                 // This is a Conditional Statement. 
                 // Basically if volumeLevel isn't null, 
                 // then it uses it's value, 
                 // otherwise it uses the DefaultVolumeLevel that we've set above.
                 volumeSlider.normalizedValue = PlayerPrefs.HasKey("VolumeLevel") ? PlayerPrefs.GetFloat("VolumeLevel") : DefaultVolumeLevel;
             }
             else
             {
                 Debug.LogError("[" + temp.name + "] - Does not contain a Slider Component!");
             }
             
         }
         else
         {
             Debug.LogError("Could not find an active GameObject named Volume Slider!");
         }
 
     }
 
     /// <summary>
     /// Called when the player clicks the Apply Button, this saves all of the values.
     /// </summary>
     public void OnApply()
     {
         // Set the VolumeLevel float.
         PlayerPrefs.SetFloat("VolumeLevel", volumeSlider.normalizedValue);
     }
 }


Just a picture of my Unity Instance to show what I had.

alt text

Comment
Add comment · Show 3 · 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 Khorkhe · Sep 20, 2014 at 11:47 PM 1
Share

Thanks again Zephrim, you were right about an error interfering with the results. Fixed it.

avatar image Zephrim · Sep 21, 2014 at 12:05 AM 0
Share

Glad I was able to help!

avatar image gregroberts · Dec 23, 2014 at 12:21 AM 0
Share

can you also supply a JavaScript solution?

avatar image
0

Answer by duchv · Nov 07, 2014 at 06:51 AM

using UnityEngine.UI;

did you import it?

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
avatar image
0

Answer by gregroberts · Dec 23, 2014 at 04:56 AM

Here's the solution, in JavaScript: Be VERY careful with the syntax, its exxtremely case sensitive.

 import UnityEngine.UI;
 var mySlider: UnityEngine.UI.Slider;
 
 function Start () {
      // substitute 'sliderName' with the literal name 
      // of your slider as it appears in the hierarchy:
      mySlider = GameObject.Find("sliderName").GetComponent(UnityEngine.UI.Slider);
 }
 
 function Update () {
      mySlider.value = n; 
      // insert desired variable here in place of 'n'
      // slider will automagically adjust to display new value
 }
 
Comment
Add comment · Show 2 · 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 LexGear · Mar 04, 2015 at 10:16 AM 0
Share

What does "import UnityEngine.UI;" do?

avatar image Vickylance LexGear · Jul 06, 2016 at 07:58 AM 0
Share

@LexGear It is like using UnityEngine.UI; in C# it basically imports all the functions and classes that belongs to the UI Namespace into the current script so that we can work with the Unity's new UI system in the current script in which it is imported. It's pretty basic I would recommend you to go through some C# tutorials to get familiar with the namespace concept

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

6 People are following this question.

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

Related Questions

Unity 4.6 Change the content from a text Object through script 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Unity 4.6 UI - change image's source image via script 4 Answers

Is it faster to set a variable every frame or check if it needs to be changed? 3 Answers

Application.OpenURL not working 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