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 Maccyjam · Jan 04, 2013 at 04:55 PM · screenvariables

Screen.width/2 Giving an Incorrect Value

I'm trying to declare a variable to set the position of a button as 'Screen.width/2'. When doing so, Unity gives the value of the variable in Inspector view as 154 whereas the Screen's width in Maximize on Play mode is about 909 so the button is positioned too far to the left.

I have used 'Screen.width/2' before in a variable, yet it was in a function whereas this time I am declaring the variable outside of the function.

What could I be doing wrong? (Also, sorry for any missing details, I'm fairly new to this whole Unity thing!)

Edit: Of course, the code would probably be helpful, sorry about that:

 #pragma strict
 
 function Start () {
 
 }
  //button width
 var buttonW : int = 100;
 //button height
 var buttonH: int = 50;
 
 //Half of Screen width
 var halfScreenW : float = Screen.width/2;
 //Half of Button width
 var halfButtonW : float = buttonW/2;   
 //Declare variable 'customSkin' as type GUISkin
 var customSkin : GUISkin;
 
 function OnGUI () {        
     //Set variable 'customSkin' as the main GUI.skin. (customSkin is assigned in Unity Inspector)
     GUI.skin = customSkin;
     //Create button and detect if clicked. Creating button at midpoint of screen so that the midpoint of button is directly at midpoint of screen. Y is set to 560 as it drops below the title image.
     if(GUI.Button(Rect(halfScreenW-halfButtonW,560,buttonW,buttonH),"Play Game"))
     {
         //print("You clicked me!");
         Application.LoadLevel("game");
     }
 
 }

As you can probably tell, it is tutorial sample code so I probably haven't fully understood it.

Comment
Add comment · Show 1
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 Piflik · Jan 04, 2013 at 05:00 PM 0
Share

Would help to see the code...

2 Replies

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

Answer by Dave-Carlile · Jan 04, 2013 at 05:02 PM

Are you setting the variable at the same time you declare it? If so, and it's a public property that shows up in the inspector, whatever the initial value was when you added the script will persist because it's serialized that way.

If you want the value to change based on the actual screen size you should set it in the Awake or Start functions.

Comment
Add comment · Show 5 · 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 Maccyjam · Jan 04, 2013 at 05:19 PM 0
Share

I have set it as I declare it and it does appear in the inspector. I have set it outside of a function and it is being used by the OnGUI function.

avatar image Dave-Carlile · Jan 04, 2013 at 05:21 PM 0
Share

You need to set it in one of the functions I mentioned, or calculate in OnGUI. Since they're in public variables they're retaining whatever their initial value was. That's how the editor works - it saves the public properties.

avatar image Maccyjam · Jan 04, 2013 at 05:27 PM 0
Share

Ah, placing it in OnGUI got it working. I also tried placing it in Start however that gave me an error stating that the variable used in OnGUI didn't exist. I assume that is because when a variable is declared in a function, it can only be used in that function?

Anyway, thanks for your help!

avatar image Dave-Carlile · Jan 04, 2013 at 05:33 PM 0
Share

Well, you would still need to declare the variable at the top. You would just set the variable in the function.

 // declare it at the top
 var halfScreenW : float;

 // set it in the start function
 function Start()
 {
   halfScreenW = Screen.width/2;
 }


But, doing it all in OnGUI is fine too. It's likely not an expensive calculation.

avatar image Maccyjam · Jan 04, 2013 at 05:43 PM 0
Share

Ah, I see. Will it take up less system resources doing that way? If so, then I'll keep that in $$anonymous$$d.

avatar image
0

Answer by Piflik · Jan 04, 2013 at 05:01 PM

When are you assigning the value to that variable? in Start()? It is possible that the screen width doesn't have the maximized value there and the variable doesn't get updated.

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 Maccyjam · Jan 04, 2013 at 05:17 PM 0
Share

I'm assigning it outside of a function, would that cause the same thing as described?

avatar image Piflik · Jan 04, 2013 at 05:21 PM 0
Share

Probably. Also your variables are public, which means they are exposed in the Inspector, like Dave said. They will have a value assigned when the script first compiles and use that value unless you change it. You should either update that variable whenever the resolution is changed, or use Screen.width directly in your GUI code, without assigning it to a variable first.

avatar image Maccyjam · Jan 04, 2013 at 05:28 PM 0
Share

Declaring the variable and using it in the OnGUI function got it working, although I'm unsure whether that would update the variable whenever the resolution changes. Thanks for your help with this!

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

10 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

Related Questions

how to access a screen(separate obj in my app). 1 Answer

Prefab instantiate 1 Answer

Loading screen? 2 Answers

Display compatibility between windows 7 and xp 1 Answer

Android fullscreen change 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