Should I be declaring variables within onGUI()?

If I do “ww = Screen.width” outside of OnGUI() it gives me the wrong value, but within it, it gives me the right values.

For example:

var ww : float = Screen.width; //gives me 200

function OnGUI(){
    var ww : float = Screen.width; //gives me 1600

But I feel like declaring a new variable every frame is taxing. How else can I do this?

The whole point of using Screen.width and height is so you can dynamically resize the GUI element based on the size of screen. This means you want to use the one from inside OnGUI since its set while the game is playing. if you’re worried about the overhead of creating a variable each frame ( you shouldn’t) then just set the value of ww inside OnGUI().

var ww : float = Screen.width; //instantiate it
function OnGUI()
    ww = Screen.width; //Set it each frame

but honestly the cost of creating a variable is tiny. Also Screen.width and height are ints. Just a heads up if you ever switch to c# and variable types matter.