- Home /
Instantiate rect transform objects
Hi, I have a UI prefab that have Pos X value of 111 and PosY value of -158
However when I instanciate it wih
GameObject prefab = Instantiate(CardPrefab, CardPrefab.transform.position, CardPrefab.transform.rotation) as GameObject;
It lost the recttransofrm values and give me a pos x value of 0 and PosY vlaue of 0 ( it is like completly reset)
How can I instantiate it with the same Rect Transform values as prefab?
Answer by allenallenallen · Apr 04, 2017 at 02:10 PM
Shouldn't matter. You should use anchoredPosition to control the position of UI, not transform.position.
Read the API: https://docs.unity3d.com/ScriptReference/RectTransform-anchoredPosition.html
I use anchored position , but they reset as well, on my prefabs my anchors $$anonymous$$ is 0 - 1 and max 0-1, but when I Instantiate it they reset to $$anonymous$$ 0-0 and max 0-0
What about setting the parent of the instantiated UI?
prefab.transform.SetParent(anotherTransform, false);
Reference: https://forum.unity3d.com/threads/problem-with-instantiate-ui-prefabs.271345/
transform.SetParent(content.transform, false); transform.SetParent(content.transform, true); transform.parent = content.transform;
But even if it change the offset(even changed, they are different from prefabs) , the anchors, scale , and pivot are still reset
Your answer
Follow this Question
Related Questions
Instantiated UI prefab in canvas does not render image 1 Answer
UI LayoutGroup does not organize dynamically instantiated buttons 8 Answers
Instantiating UI element on Screen space - Overlay not instantiating exact specified location 0 Answers
Having trouble with cloning with prefabs 1 Answer
Is there a way to Instantiate a button prefab as a child of a Canvas? 2 Answers