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
0
Question by Broken Crayon Games · Jan 04, 2015 at 04:35 PM · screen.widthhorizontal-slider

Screen.width Not Working

Hey everyone,

I'm trying to make a slider bar along the bottom of my screen with a length equal to 1/2 the width of the screen. I have sliders on the left & right of my screen properly functioning as vertical sliders, however the horizontal slider appears in the center of my screen (telling me it is calculating screen.width properly), however, the slider itself is not the proper length (keeps appearing as approximately 1/10 screen.width) and I have no clue why!

Any help would be much appreciated. Here is my code:

 using UnityEngine;
 
 using System.Collections;
 
 public class GUI : MonoBehaviour {
     
     #region Variables
     //Slider Control Values
     public float sliderMin = 0.0f;
     public float sliderMax = 360.0f;
     public float xSliderValue;
 
     //Slider Layout Variables
     public int horzSliderLength = (Screen.width / 2);
     public int horzSliderWidth = 15;
 
     //General GUI Layout
     public int screenMargin = 10;
     public int sliderLabelWidth = 40;
     public int sliderLabelHeight = 25; 
     #endregion
 
 void OnGUI ()
     {
     #region Sliders
         //X-axis Slider (bottom middle of screen)
         xSliderValue = GUI.HorizontalSlider(new Rect((Screen.width / 2) - (horzSliderLength / 2), (Screen.height - ((horzSliderWidth / 2) + screenMargin + sliderLabelHeight)), horzSliderLength, horzSliderWidth), xSliderValue, sliderMin, sliderMax);  
     #endregion
     }
 }

THANK YOU!

Comment
Add comment · Show 2
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 Qasem2014 · Jan 04, 2015 at 06:41 PM 0
Share

change public int horzSliderLength to public float horzSliderLength !!!!

maybe work :)

avatar image Broken Crayon Games · Jan 04, 2015 at 07:15 PM 0
Share

Thank you for the suggested fix! I tried it already and unfortunately it didn't work. :-(

2 Replies

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

Answer by tanoshimi · Jan 04, 2015 at 07:26 PM

Your horzSliderLength is currently a public variable, which means that its value will be being set by whatever is assigned in the inspector, overwriting the (Screen.width / 2) you've initialised in code.

The solution is simple - make it a private variable.

 private int horzSliderLength = (Screen.width / 2);
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 Mmmpies · Jan 04, 2015 at 07:34 PM 0
Share

$$anonymous$$eep doing this, not plagiarizing @tanoshimi just tested it and didn't hit refresh before posting my answer!

avatar image Broken Crayon Games · Jan 05, 2015 at 02:21 PM 0
Share

@tanoshimi & @$$anonymous$$mmpies - Thank you for the help! Worked perfectly. I knew it was something stupid I was overlooking.

avatar image
0

Answer by Mmmpies · Jan 04, 2015 at 07:32 PM

Change this to private:

 private int horzSliderLength = (Screen.width / 2);

and see what you get.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

how to make a Horizontal slider & toggle in C# with GUI textures 1 Answer

Make area adapt to screen size 1 Answer

C# code is making 0.0 a double and not a float 3 Answers

Positioning across various resolutions 0 Answers

GUI slider and rotation 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