Can someone explain...

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.

public class MainMenuGUI : MonoBehaviour 
{
    //Buttons Variables
    public float btnWidth = Screen.width / (float)1.66;
    public float btnHeight = Screen.width / 7;
    public float heightPos = 150;

    public Rect buttonRect = new Rect(Screen.width / 2 - (Screen.width / (float)1.66) / 2 + 50, Screen.height / 2 - 25, btnWidth , btnHeight);

    void OnGUI()
    {
        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"))
        {
            print("You clicked the button!");
        }
    }
}

The errors I get when I pass variables into new Rect instead of the actual value.

Try declaring them where they are and assigning values in the Start() function.

1 Like

I will give it a shot when I get home.
Thanks

No problems!

Yeah, Can’t use one variable in the declaration of another, outside of a function.

Oh yeah, it makes sense now that you say.

Yeah it worked thanks, not sure how I missed that. :smile: