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 Rgalaxy · Jan 14, 2014 at 11:23 AM · javascriptguierrorcalculation

GUI error calculating

so, i create a gui like this..

 #pragma strict
 var bg : Texture;
 var bg_w : float = Screen.width*0.75;
 var bg_h : float = Screen.height*0.9;
 var posbg_w : float= Screen.width*0.1;
 var posbg_h : float= Screen.height*0.05;
 var b_width:float= 420;
 var b_height:float=100;
 
 
 //konfigurasi button_perkalian
 var button_perkalian: GUIStyle;
 var pos_b1w: float = posbg_w+(bg_w*0.1);
 var pos_b1h: float = posbg_h+(bg_h*0.05);
 //konfigurasi button_luas
 var button_luas: GUIStyle;
 var pos_b2h: float = pos_b1h+100;
 
 //konfigurasi button_satuan
 var button_satuan: GUIStyle;
 var pos_b3h: float = pos_b2h+100;
 function Start () {
     print(Screen.width);
 }
 
 function OnGUI (){
     GUI.DrawTexture(Rect(posbg_w,posbg_h,bg_w,bg_h),bg);
     GUI.Button(Rect(pos_b1w,pos_b1h,b_width,b_height)," ",button_perkalian);
     GUI.Button(Rect(pos_b1w,pos_b2h,b_width,b_height)," ",button_luas);
     GUI.Button(Rect(pos_b1w,pos_b3h,b_width,b_height)," ",button_satuan);
 }

i know this code must work.. but why it's have a different value on Inspector? i print the screen.width and says it was 726.. if i times it with 0.75 it will become 554, but in the inspector, it says 175.5 ! i don't know why this is happening, anyone could explain me?

alt text

capture.png (15.7 kB)
Comment
Add comment · Show 3
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 Rgalaxy · Jan 14, 2014 at 02:03 PM 0
Share

please answer in the answer section, i can't accept it here... :3 but thanks very much @ziv03 u really do me a favor here..

i don't sure why but it's working this way..

avatar image ziv03 · Jan 14, 2014 at 03:13 PM 0
Share

Sure, I converted it to an answer.

The reason it works this way is because the scripts are comiled before you run the game. so lets say you wrote it outside the Start function, after the script is comiled the value will always be the same, without any relation to the screen bounds. the Start and Awake functions are called after the gameobject is initialised.

avatar image Rgalaxy · Jan 14, 2014 at 06:25 PM 0
Share

i understand now, thanks so much @ziv03 i'll keep this to remember .. :D

2 Replies

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

Answer by ziv03 · Jan 14, 2014 at 12:01 PM

the values that aren't in the start or in the onGUI functions are pre-compiled. when you run the game, the values stay the same no matter what the Screen bounds are. get all the values in the Start function.

 function Start()
 {
    bg_w = Screen.width*0.75;
 }
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 HappyMoo · Jan 14, 2014 at 12:01 PM

During Initialization it probably takes the screenwidth of the game view. Try Maximizing while play if you need this to be the whole screen.

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 Rgalaxy · Jan 14, 2014 at 02:05 PM 1
Share

yeah, i've try this too, but the problem still there, the calculation still odd and my gui is so wrecked.. xD

and it comes up, the problem i should have initiate the number on Start function :)

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

20 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

Related Questions

Error: you are pushing more GUIClips than you are poping. 1 Answer

Setting Scroll View Width GUILayout 1 Answer

JavaScript OnGUI GuiSkins aren't working, multiple scripts 1 Answer

How do I change the text of a gui image text 1 Answer

4.6 Trigger.js Wrong command is running. Help? 0 Answers


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