GUI scaling?

Hello. I developed my GUI using absolute pixel positions. I was told that using this script in my OnGUI functions would scale it to any resolution, but I am not noticing any difference? How would I get this bit of code to work? Thanks

GUI.matrix = Matrix4x4.Scale(Vector3(644 * Screen.width, 966 * Screen.height,1));

The code should actually be:-

GUI.matrix = Matrix4x4.Scale(Vector3( Screen.width / 644, Screen.height / 966,1));

You should put it in OnGUI just before drawing the part of the GUI that you want to scale (put it as the first line of the function if you want the whole GUI to scale).

I put that code into an on GUI functions and I got this error
“Ignoring invalid matrix assinged to GUI.matrix - the matrix needs to be invertible. Did you scale by 0 on Z-axis?”

Here is the script with the code in it:

var mySkin : GUISkin;

var G : GameObject;
var S : GameObject;
var A : GameObject;

var toolbarInt = 0;
var toolbarStrings : String[] = ["", "", ""];

function OnGUI () {
	GUI.skin = mySkin;
	GUI.matrix = Matrix4x4.Scale(Vector3( Screen.width / 644, Screen.height / 966,1));
        toolbarInt =   GUI.Toolbar (Rect (35, 110, 900, 30), toolbarInt, toolbarStrings);
}

function Update(){
	if (toolbarInt==0){
                  G.SetActiveRecursively (true); S.active=false; A.active=false;
       }
	if (toolbarInt==1){
                  G.SetActiveRecursively(false); S.active=true; A.active=false;
      }
	if (toolbarInt==2){
                 G.SetActiveRecursively(false); S.active=false; A.active=true;
      }
}

There is another forum that discusses that problem exactly. See here:

http://forum.unity3d.com/threads/62792-Invalid-Matrix

Basically, you have to turn the Screen.width and the number it is divided by into floats, not intergers.

How do I know what resolution to use in that script? I played my scene in the free aspect ratio and opened statistics, and it told me 972 X 636. I put those numbers in the code:

GUI.matrix = Matrix4x4.Scale(Vector3( parseFloat(Screen.width)  / 972, parseFloat(Screen.height) / 636));

Am I doing this correctly? I am still getting that error, but when I change the scene player to a different aspect ratio, the GUI elements seem to go close to where I want them.