Constructors

How or is it possible to make a consrtuctor with x,y,width.height all in the same variable. for gui elements. :smile:

The x, y, width and height values generally are within one variable of type Rect. You can set the Rect variable up in advance of the GUI function call if it works better that way in your script:-

var buttonRect: Rect = Rect(10, 10, 200, 150);
GUI.Button(buttonRect, "Click me");

If you want to pass the four values without explicitly using a Rect variable at all, you will need to declare your own function that takes the values, builds a Rect from them and then calls the original GUI function.

Thank you andeeee this is exactly what i was looking for. a way to compact parameters in the inspector. :smile: I’m just learning about constructors and the way i learned how is not working out in unity. Could you give me an example of how to create my own? Here is the way i read how to do it.
:arrow:

function Square(xPos,yPos,width,height){
   x = xPos
   y = yPos
   width = width
   height = height
}
var mySquare = new Square(5,5,9,9);

But i get errors about x y not being members of a square. What would be the correct way to create this type of constructor? with four parms?

In Unity’s JS, a constructor is defined on a class:-

class MyLeanClass {
   var x: float;
   var y: float;

   function MyLeanClass(x: float, y: float) {
      this.x = x;
      this.y = y;
   }
}

You can then call the constructor to initialise the variable:-

var mlc = new MyLeanClass(100, 200);

Classes defined in JS are automatically serialisable, which means their variables will appear in the inspector like a built-in class.

Defining a class using a function, as in your example, is a feature of other flavours of JS (especially browser-based ones), but this isn’t possible in Unity.