2
\$\begingroup\$

So, I am trying to make a simple GUI Instantiate system. The GUI is going to spawn successfully, but the position is wrong.

Position : (Left, Right: 249.8423 ; Top, Bottom : 400) Anchors : (Min: X:0 Y:0 ; Max: X:1 Y:1) Pivots : (X:0.5 Y:0.5) Rotation : (X:0 Y:0 Z:0) Scale : (X:1 Y:1 Z:1) always... 

My Code :

 GUIPausePopup pause = UI.Create<GUIPausePopup>(GameObject.Find("Canvas")); //Spawn and set parent(Canvas) pause.GetComponent<Transform>().gameObject.SetActive(true); //Working without any issues. //Position system not work. I don't know why. pause.GetComponent<RectTransform>().anchoredPosition3D = new Vector3(0f, 0f, 0f); pause.GetComponent<RectTransform>().anchoredPosition = new Vector2(0f, 0f); pause.GetComponent<RectTransform>().localPosition = new Vector3(0f, 0f, 0f); pause.GetComponent<RectTransform>().position = new Vector3(0f, 0f, 0f); pause.transform.position = new Vector3(0f, 0f, 0f); pause.transform.localPosition = new Vector3(0f, 0f, 0f); 

Thanks for help...

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Unity UI layouting problems are notoriously hard to troubleshoot without a complete picture of everything that is happening in the UI... Are there any layout-related components on the object or one of its parents? \$\endgroup\$ Commented Jun 2, 2016 at 14:05
  • \$\begingroup\$ pretty sure you need to set anchorMin and anchorMax before seeting anchoredPosition. \$\endgroup\$ Commented Jun 2, 2016 at 14:45
  • \$\begingroup\$ I'm having this same issue... 4 years later x_x \$\endgroup\$ Commented Apr 30, 2020 at 2:46

1 Answer 1

1
\$\begingroup\$

4 years later but I bring an answer that solved it for me.

This is using a World Space Canvas and I was trying to instantiate a prefab (menu)

menu.GetComponent<RectTransform>().anchoredPosition3D = this.GetComponent<RectTransform>().anchoredPosition3D; 

"this" being the parent

I think what was messing it up for me was that since I'd been at this for a while I tried the same thing you did: after calling this I had other things trying to reset it's position and those were the ones causing issues! Just do anchoredPosition3D and nothing else and it should work - if your situation was like mine.

EDIT: As @DMGregory explains in the comments the correct way would be:

((RectTransform)(menu.transform))anchoredPosition3D = ((RectTransform)transform).anchoredPosition3D; 

But one year ago the code that I included here worked for me.

\$\endgroup\$
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.