I trying a method of instatiation TMP inside of canvas. When my prefab appears it's size different from original prefab, I don't know what is the problem.
public GameObject MyParent;
public GameObject MyChild;
public void Start()
{
GameObject mChild = Instantiate(MyChild);
mChild.transform.parent = MyParent.transform;
}
CodePudding user response:
This is a common phenomenon while changing parent of any transform.
Explained with solution -
In Unity, the position, scale and rotation of any object in Hierarchy is with respect to it's parent.
That is why your "MyChild" object's size is getting updated as soon as you make it child of your canvas.
To Fix This - Store the size of original object (say = originalSize) and after changing parent of the object, set size of new object to originalSize variable
public GameObject MyParent;
public GameObject MyChild;
private Vector3 originalSize;
public void Start()
{
GameObject mChild = Instantiate(MyChild);
originalSize = mChile.transform.scale;
mChild.transform.parent = MyParent.transform;
mChild.transform.localScale = originalSize;
}
This should fix the issue.
