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 jesser1981 · Dec 22, 2013 at 04:01 PM · guimenugui-button

GUI button off centre

Hi, I'm brand new to scripting, I've been trying to do tutorials but kept coming across things that weren't working and I am not learning anything that way so decided to just learn from scratch and use the scripting reference. That being said I am just trying to start out small and build a GUI menu. The problem am I am having is I can't seem to get the button in the centre of my screen no matter what I have tried.

The button is in the middle of my screen when the game window in minimized but soon as I maximize it it's no longer in the middle of my screen. I am going to provide screen shots and the code of what I have.alt text well just off centre. alt text

 using UnityEngine;
 using System.Collections;
 
 public class MyGui : MonoBehaviour 
 {
 
     public float screenH = Screen.height*.5f;
     public float screenW = Screen.width*.5f;
     public float buttonW = Screen.width * .1f;
     public float buttonH = Screen.height * .1f;
 
     // Use this for initialization
     void Start () 
     {
 
 
 
     
     }
     
     // Update is called once per frame
     void OnGUI () 
             {
                 //Make menu box
         //    GUI.Box (new Rect (10,10, 150, 250),"Main Menu"); 
             {
 
                         //This is a button
             if (GUI.Button (new Rect (screenW, screenH, buttonW, buttonH), "Button")) 
                     {
                     Debug.Log ("You clicked Me!");
                     }
             }        
                 
             }
     
 }


Any help or advice would be greatly appreciated.

centredmingamewindow.png (143.0 kB)
maxgamewindownotcentred.png (51.2 kB)
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 JSierraAKAMC · Dec 22, 2013 at 04:42 PM 0
Share

I am not going to post this as an answer, as I do not know if it will work, but try turning the aspect ratio to 16:9 by clicking where it says "Free Aspect" and turn it to 16:9.

1 Reply

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

Answer by KellyThomas · Dec 22, 2013 at 04:57 PM

You initialize some of your variables in the class declaration:

 public float screenH = Screen.height*.5f;
 public float screenW = Screen.width*.5f;
 public float buttonW = Screen.width * .1f;
 public float buttonH = Screen.height * .1f;

These will be run "early" in the application lifecycle, possibly before Screen is fully initialized.

Try assigning the values during the start method:

 public class MyGui : MonoBehaviour 
 {
     public float screenH;
     public float screenW;
     public float buttonW;
     public float buttonH;
  
     // Use this for initialization
     void Start () 
     {
         screenH = Screen.height * 0.5f;
         screenW = Screen.width * 0.5f;
         buttonW = Screen.width * 0.1f;
         buttonH = Screen.height * 0.1f;
     }
 
 // ... the rest of your class ...
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 jesser1981 · Dec 22, 2013 at 05:42 PM 0
Share

You were pretty close actually, they had to be declared inside the OnGUI method.

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

19 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

Related Questions

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

Bringing window to back GUI.BringWindowToBack() 0 Answers

Cases if a GUI.Button is clicked two or more times?? 2 Answers

How to make a GUI Button shrink when pressed 4 Answers

How can i control the Camera like This? 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