My aim was to change the btns postion depending on the screen orientation. However, it was causing a gdb crash in xcode. My thinking was somehow my continous call for leftBtn in the 2 if statements caused this becuase if they are removed, it runs fine.
So I wanted to declare leftBtn as a class var however to no avail…
I did so like this…
var portraitRect:Rect;
var LandscapeRect:Rect;
var leftBtn;
function OnGUI()
{
if(Screen.orientation == ScreenOrientation.Portrait)
{
leftBtn = GUI.RepeatButton (PortraitRect, leftBtnImage, guiStyle);
print("P");
return;
}
if(Screen.orientation == ScreenOrientation.LandscapeLeft)
{
leftBtn = GUI.RepeatButton (LandscapeRect, leftBtnImage, guiStyle);
print("LSL");
return;
}
}
Then when you look in the editor, you can just set the rectangles that way. This way you can also hit the play button, and drag the numbers to what you want, take note of them, stop the game and insert the new numbers, to get exactly where you want it, visually. Also, shouldn’t cause any problems when compiling.
My problem is… is there a way I can declare the var leftBtn, and only have it exist in the correct if statement? Meaning, that currently, tho I can not see it, as its dimensions are 0, 0, and I can move it off screen by giving it outlandish coordinates, is there a way I can insure that it does not exist out side the if statement.
Currently it does. I know this because if I make it have visible dimensions, it can be seen along side the btn in the if statement.
The RepeatButton method returns boolean, not Rect. In your case leftBtn should cause errors in your code, since you declare it above as Rect but then treat it as boolean to store the return of RepeatButton. [EDIT] I just noticed you don’t declare its type, so it won’t return errors, but still you’re using it wrong.
As such, you cannot store a button in a variable. This is deliberate from Unity. The current GUI system is not supposed to be object based, it is supposed to be state based. Buttons are not instances in your scene and get destroyed (and then re-drawn) when the frame ends.
And therefore, you cannot treat them as instances. As for your specific issue, make your Rect values resolution independent. Unity sorts all GUI elements automatically when orientation switches, and as long as your dimensions are resolution independent they are going to keep their proper place. If however you wish to place them in a different way when the orientation changes, do what was suggested to you above and only change the Rect coordinates.
Technically speaking, yes, you could. If you don’t define a type, it’s set to a type of object. An Object is able to hold multiple types of variables without being cast to a specific type. It makes it easier to use if you don’t know what the type is, but uses a lot more ram. It’s best to cast types so that you keep ram consumption low.
GUI.Button is not an object, it is a static method that both draws the button and returns a boolean denoting whether or not the button has been pushed in that frame.
Thanks.
It seems a bit much to do it that way (@avidesk). I am fine just declaring it as false, then using it as long as not too much RAM is being eaten. I set it to a Bool type but that crashes it (XCode). Just a test.