Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity scripted button text not locked to button

Discussion in 'Scripting' started by Fireking883, Jan 3, 2016.

  1. Fireking883

    Fireking883

    Joined:
    Nov 28, 2014
    Posts:
    50
    Sorry if there is an obvious answer to this question, but have locked the button to the center of the screen, but when I play with a resolution other than my monitors resolution, the button(s) moved around the screen while the clickable part of the button stays in the center. Here is my OnGUI script:

    Code (JavaScript):
    1.   function OnGUI(){
    2.         if (GUI.Button(Rect(0, 20, 300, 30), "You are on level "+LevelSaveNumber)){
    3.             if(LevelSaveNumber == 0){
    4.                 Application.LoadLevel("Tutorial");
    5.             }
    6.            
    7.             if(LevelSaveNumber == 2)
    8.                 Application.LoadLevel("Tutorial");
    9.     }
    10.             if (GUI.Button(Rect(Screen.width - 200, 20 , 200, 30), "Delete Memory")){
    11.                 PlayerPrefs.DeleteAll();
    12.                 LevelSaveNumber = PlayerPrefs.GetInt("LevelSaveNumber");
    13.         }
    14.         if (GUI.Button(Rect(Screen.width/2-100, Screen.height/2 - 30,100, 30), "Exit")){
    15.             Application.Quit();
    16.         }
    17. }
    18. }
    Thanks in advance for any help!
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    OnGUI is the legacy UI system, it was superseded by the canvas based system in 4.6+ (except for editor UI). If you are not particularly attached to what you have already it might be worth looking into the new system, it's far easier to configure and use: https://unity3d.com/learn/tutorials/topics/user-interface-ui
     
  3. Fireking883

    Fireking883

    Joined:
    Nov 28, 2014
    Posts:
    50
    Yes, I did originally use this, but I had a problem getting the buttons to stay in there place on different sized screens, so I decided to go with OnGUI, but you see where that got me, but I probably just overlooked something as GUI has never really mattered to me before. Thanks!