Problem with using Rect.set(), it does nothing.
When using Rect.set(), the rect does not change. I don't know how else to better explain it. Here is the code, with unimportant parts removed.
RectTransform DialogueBoxRect = new RectTransform();
void Awake()
{
Player.DialogueBox = GameObject.Find("DialogueBox");
if(Screen.width < 1280)
{
DialogueBoxRect.rect.Set(0, 14, Screen.width - 20, 300); // Dialogueboxrect HAS already been defined and set
}
}
It returns no errors, but it does absolutely nothing. It should be changing the dialogue box's size to less than the screen, so that it can played in the gameview and be read, but it is still too big. Any help is appreciated, thanks in advance.
Answer by hexagonius · Dec 02, 2016 at 11:01 PM
the problem is you're changing the rect of DialogueBoxRect, which you are creating though the constructor which is not allowed and won't do anything because this way it's not even a part of a gameobject. I guess you should instead be doing something like
Player.DialogueBox.GetComponent<RectTransform>().rect.Set(...
@hexagonius , I feel stupid now for not seeing that but it still is not working. This is what I am using now.
private void Start()
{
if(Screen.width < 1280)
{
Player.DialogueBox.GetComponent<RectTransform>().rect.Set(0, 14, Screen.width - 20, 300);
}
}
Do you have any idea what could be wrong?
well, since I don't know if "DialogueBox" is even the right RectTransform, I don't know. But I have a clue. You're setting the rect once via code, but the RectTransform will be updated with every change made in any UI Element under its canvases hierarchy. I haven't used my brain really, so here is a better Idea altogether:
You can achieve the same effect without coding, which in this case is cleaner. Select the GameObject with the RectTransform. click the mill-like rectangle in the upper left that has "middle" and "center" written to it. It should show the anchor presets. Choose the bottom right (blue arrows). What this does is stretch the whole Rect fullscreen no matter the window size. The position and size informations to it's right should now read top left bottom and right. This is where you put your indentations you tried setting in your code. From now on, the actual Rect will keep its distance from the borders forever.
Your answer
Follow this Question
Related Questions
Convert RectTransform.rect to Rect world 3 Answers
Place GameObject's rect 0 Answers
A strategy for puzzle games: Transform or RectTransform or both? 0 Answers
Helpp !! Can't draw the rectangle on the gameobject 's positon 0 Answers
Resize UI Panel to the same size as its parent Canvas through code 0 Answers