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. Dismiss Notice

Can someone explain...

Discussion in 'Scripting' started by boowman, Jul 16, 2014.

  1. boowman

    boowman

    Joined:
    Jan 3, 2014
    Posts:
    57
    Hello
    I tried to do this using a public variable but for some reasons if I use the variable the values are not updating.
    Also I tried to pass values into Rect from variables but I get an error.
    Code (CSharp):
    1.  
    2. public class MainMenuGUI : MonoBehaviour
    3. {
    4.     //Buttons Variables
    5.     public float btnWidth = Screen.width / (float)1.66;
    6.     public float btnHeight = Screen.width / 7;
    7.     public float heightPos = 150;
    8.  
    9.     public Rect buttonRect = new Rect(Screen.width / 2 - (Screen.width / (float)1.66) / 2 + 50, Screen.height / 2 - 25, btnWidth , btnHeight);
    10.  
    11.     void OnGUI()
    12.     {
    13.         if (GUI.Button(new Rect(Screen.width / 2 - (Screen.width / (float)1.66) / 2, Screen.height / 2 - (Screen.width / 7) - 100, Screen.width / (float)1.66, Screen.width / 7), "Play"))
    14.         {
    15.             print("You clicked the button!");
    16.         }
    17.     }
    18. }
    19.  
    The errors I get when I pass variables into new Rect instead of the actual value.

     
  2. TRALLALAL

    TRALLALAL

    Joined:
    Sep 7, 2013
    Posts:
    132
    Try declaring them where they are and assigning values in the Start() function.
     
    LuisDeliz likes this.
  3. boowman

    boowman

    Joined:
    Jan 3, 2014
    Posts:
    57
    I will give it a shot when I get home.
    Thanks
     
  4. TRALLALAL

    TRALLALAL

    Joined:
    Sep 7, 2013
    Posts:
    132
    No problems!
     
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Yeah, Can't use one variable in the declaration of another, outside of a function.
     
  6. boowman

    boowman

    Joined:
    Jan 3, 2014
    Posts:
    57
    Oh yeah, it makes sense now that you say.
     
  7. boowman

    boowman

    Joined:
    Jan 3, 2014
    Posts:
    57
    Yeah it worked thanks, not sure how I missed that. :D