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 shezab · Jun 28, 2012 at 09:14 PM · guigui-buttongui-skin

GUI Button screen Width and Centering Problem

Currently I am having some trouble with my GUI button display. First i can't see the button when i play without maximizing, button don't render in the middle of the screen or in other words i can't even see it, and even if I switch to a widescreen or change the resolution, though i could see the button but its on the left hand side. Is their a solution for this...?

The code is as under:

**var customSkin:GUISkin; var buttonW:int = 100; var buttonH:int = 50; var halfScreenW:float = Screen.width/2; var halfButtonW:float = buttonW/2;

 function OnGUI () {
 
 GUI.skin = customSkin;

if(GUI.Button(Rect(halfScreenW-halfButtonW,560,buttonW,buttonH), "Play"))

{ Application.LoadLevel("game"); }**

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 whydoidoit · Jun 28, 2012 at 09:16 PM 0
Share

Does it work without your custom skin?

4 Replies

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

Answer by Loius · Jun 28, 2012 at 09:18 PM

If that's all your code, halfScreenW is public, which means it doesn't change unless you change it yourself in the inspector. If you make halfScreenW a private var, I believe it will update correctly when you start the game.

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 whydoidoit · Jun 28, 2012 at 09:19 PM 0
Share

Oh good spot :) Actually needs to be set in start doesn't it or be a local variable.

avatar image
1

Answer by Mortoc · Jun 28, 2012 at 09:20 PM

I find that it's more flexible to just calculate the screen coords every frame so that when the resolution changes, weirdness doesn't ensue:

 function OnGUI() {
     GUI.skin = customSkin;
     if( GUI.Button( Rect(
             (Screen.width - buttonSize.x) * 0.5f,
             (Screen.height - buttonSize.y) * 0.5f,
             buttonSize.x
             buttonSize.y
         ))
      ) {
         Application.LoadLevel("game");
     }
 }
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 shezab · Jun 28, 2012 at 09:21 PM

Let me check it out and i,ll get back to you.....

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 shezab · Jun 28, 2012 at 09:30 PM

Oh..... Declaring both as private vars did the trick..... but can you just a bit explain ... HOW?

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 whydoidoit · Jun 28, 2012 at 09:31 PM 0
Share

Please don't use answers for comments - there's an Add New Comment button hidden on the right of the screen for that :) On Unity Answers an Answer is a Solution and not a Reply (confuses everyone the first time!)

avatar image whydoidoit · Jun 28, 2012 at 09:32 PM 0
Share

It's because the inspector saved the width of the screen when you were running in the editor when it was public - but not when it was private.

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

7 People are following this question.

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

Related Questions

Move the text in a GUI Button/Box 1 Answer

problem with gui 2 Answers

On Screen Button Controls 0 Answers

How to create a gui button inside an if statement? 1 Answer

How can you do a custom GUIStyle image position? 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