Home > Software engineering >  Want to change the Rect Transform of GameObject (Button) on canvas unity?
Want to change the Rect Transform of GameObject (Button) on canvas unity?

Time:12-20

I am working on learning how to use Unity and have been unable to solve this problem of mine. I am currently working on a prototype of a game and need to change the RectTransform Pos X to a value that I want.

However, so far I was unable to do it. I tried Vector2 and Vector3 converations but I can't seem to get it right.

Here is what I have atm (I know the new RectTransform is doing nothing, bu I am simply out of ideas.):

void Start()
{
    changeTransform = GameObject.FindGameObjectWithTag("NextButton").GetComponent<RectTransform>();
    changeTransform = new RectTransform();
}

And all I want is to set the Pos X of the rect transform to be 25. enter image description here

Anything helps. Thanks a lot.

EDIT: When I change the value of the Pos X in the inspector it gets overwritten back to the value seen in the picture.

EDIT2: Below is how my buttons look right now. I just want the Next-Button to be aligned with the other buttons on the left side without it chaning its size. enter image description here

CodePudding user response:

I can see that some properties of the Rect Transform component are being overridden by the Content Size Fitter component. You should remove the Content Size Fitter component from the GameObject or set the horizontal and vertical fit in this component to unconstrained. If this doesn't work, please let me know of what other components are attached to this GameObject.

CodePudding user response:

For what you want to achieve

I just want the Next-Button to be aligned with the other buttons on the left side without it chaning its size.

simply wrap your NextButton under another object

- NextButtonContainer
  |-- NextButton

=> make NextButton be left aligned within the NextButtonContainer

Now the NextButtonContainer can simply grow like the other buttons, have no visual components at all and allow the next button to be left aligned within it and have a fixed size

  • Related