Hello everyone,
I have a problem : I have a game object A in my scene, parented to another gameObject B, and I try to remove A from his parent B to associate it with his grand parent C (parent of B)
If I sketch :
C (a canvas)
- B (an horizontal layout group)
- A (an UI image)
to
C
- B
- A
For that I use A.transform.SetParent(C.transform, true);
But now the problem is that the position of A is completely breaked, thrown from something like [600, -200] to [214852, 254125].
I tested my scene with a breakpoint, and discovered that the position is not modified by SetParent directly, but instantly the frame after the current frame, just after all the updates methods finished their job. (Nothing suspicious in my Updates methods in my opinion)
I remarked that the anchorMin and Max changed from
min [0,0] max [1,1]
to min[0.5, 0.5] max[0.5, 0.5] in the same time.
Is anyone have an idea (or questions) ?
Thank you in advance
For reparenting UI you want that final argument to be false
.
Keep debugging. Make two things to reparent but only reparent one. When you do, also call Debug.Break()
and go look for the two objects. Where is the one that you reparented in code? Now reparent the other one by hand. How do they differ in the inspector? Can you see the second one? If you can’t see the second one, you’ll never see it if you do it in code either.
This is just standard plugging away at combinations man… keep at it!
1 Like
Oh, I didn’t try Debug.Break()
, I only tried debugging with Visual Studio.
Well, that’s what it gave : both are thrown far away, by code and by hand…
But I think I’m on a new lead : I tried disabling the component Horizontal Layout Group from the object B, and the children are all directly at the position [21312, -46872].
EDIT : yeay, that was it : the children of the horizontal layout group are instantiated by code, but I never setted their position, because I thought the horizontal layout group did. So I just had to set the position of the children just after their instantiation 
Thank you for the trick Krut-Dekker, that’s because of I found this.
That is very interesting. I believe I would have assumed exactly the same thing you did. Good to know, and glad you’re back in service.