Inventory positioning issues

Hi, I’m attempting to use this code in my inventory system. Ran into one problem that I’m having difficulties with. I’ve got:

private var buttonGroupOffSet = Vector2((Screen.width/2) - (1024/2) + 680, Screen.height - 440 );

And as I’m playing this on a set res of 1024x576, it is essentially:

private var buttonGroupOffSet = Vector2((1024/2) - (1024/2) + 680, 576 - 440 );

But the Screen.width and height don’t work correctly within this variable for some reason, but when I hard code it in, it works. This is a problem for when I choose to go full screen. This code formula works on other GUI labels and buttons I have throughout the game.

The var is used to help create this GUI:

GUI.BeginGroup (Rect (buttonGroupOffSet.x, buttonGroupOffSet.y, 350, 310));

Any suggestions are appreciated.

The problem is you’re using Screen.{width,height} from a static initializer. Those values aren’t set up at that point. Move the initialization of buttonGroupOffSet into a Start() or Awake() method, and it should work fine.

Thank you for your reply. I am still a noob at this coding. Would I change it to this:

private var buttonGroupOffSet : Vector2;

And then in the awake function:

buttonGroupOffSet = ((Screen.width/2) - (1024/2) + 680, Screen.height - 440 );

To me that seems logical but Unity doesn’t like the awake function.

Yep, that should do it. I don’t know what you mean by ‘Unity doesn’t like the awake function.’ though.

It’s Awake, not awake. All functions begin with capital letters.