Problem with instantiate UI prefabs

When I instantiate prefab with RectTransform on it in most cases it works wrong. Usually ancoredPosition and deltaSize resets and I have to set it again from script. There are another bug that I think linked to the same. I use ReferenceResolution that in all phones interface scale and looks well. And it works well with UI elements that allready exist on scene, but when I instantiate prefab with RectTransform its not scale at all. This bug has always is reproduce (((

Are you making sure to set the instance’s parent to the Canvas (or a Canvas element) using transform.SetParent(parent, false)?

63 Likes

I also struggled with that instantiation issue. It’s not so much ā€˜right’ or ā€˜wrong’, it’s just what the system does. When you use the simple version of Instantiate, the one without the position or rotation argument, then the new object just shows up at some semi-random place. I believe this is what is supposed to happen.

To instantiate an object at a specific position / rotation, you have two options:

  1. Do what you’ve been doing, use the simple version of Instantiate, and then use anchoredPosition and localPosition to relocate the new object.

  2. Use the longer call to Instantiate, the one that has both a position and a rotation argument. For example, if you want the new object to have the same position and rotation as the prefab, you do this:

newobject = Instantiate (prefabobject, prefabobject.transform.position, prefabobject.transform.rotation) as GameObject;
2 Likes

I do this:
LeaderboardSlot leaderboardSlot = Instantiate(leaderboardSlotPrefab) as LeaderboardSlot;
leaderboardSlot.transform.parent = leaderbordGrid;

The problem isn’t in ā€œwhat place the object appearsā€. Example: I have prefab, It’s Image this 10,10,0 position. It have children Text this 50,0,0 local position. When I instantiate prefab Text now have 0,0,0 local position.
Another part of the problem is that the object appears in the wrong scale (it is smaller / bigger than it should be). Its scale should establish ReferenceResolution, but he don’t for some reason

Have this problem too… I didnt see any information on how to instantiate correctly the new UI… only how to position them staticly before the game starts…

If i have a canvas and i want to instantiate any ui to it, and the resolution doesnt stay the same as when I created the prefab of the UI, it will be positioned wrong when I instantiate it in the new resolution…
Also, when using ReferenceResolution the instantiated UI wont have the right size as it should have if the UI wouldnt be instantiated (but will be there staticly)

Great news and many thanks to Senshi!

I have tried using ā€œtransform.SetParent(anotherTransform, false)ā€ instead of ā€œtransform.parent = anotherTransformā€ and it works perfectly now!

15 Likes

Hit this same issue and just to chime in…

Doesn’t scale prefab to UI scale:
go.transform.parent = this.rows.transform;

Doesn’t scale prefab to UI scale:
go.transform.SetParent(this.rows.transform);

DOES scale prefab to UI scale :):
go.transform.SetParent(this.rows.transform, false);

26 Likes

With me is even weirder, the instances appears in the editor but they are no visible, as the would appear if they were not child of canvas, what isn’t the case! any idea what is going on here?

2 Likes

Thanks so much for that! Was searching around for a bit, almost 0 info of how to solve these things. The 3rd option worked for me :slight_smile:

1 Like

This fixed my problem too. I couldn’t understand why there were such huge gaps between the items in a vertical Iayout group. I spent several hours researching this. The breakthrough came after creating manually instances of my prefab before letting the code do it and then comparing the inspector output. Now instead of googling for big gaps I was googling for wrongly sized prefab instantiations and so stumbled over this page. Thank you, Senshi, you saved me yet more hours of pain.

2 Likes

That did it for me. Thanks!

1 Like

Had to chime in on my personal problem and fix. I had a problem with the Instantiated UI Button being way too big. It does NOT follow the scale of it’s parent. My background panel was at 0.73 scale and the instantiated button was at 1.0. Don’t scale your UI’s, readjust their widths and heights.

So I’m always makin simple mistakes… here’s what I finally realized when none of these worked… my prefab texts were children of another canvas in my project folder… when it was reading the transform for instantiation, it was pickin up the 0,0,0 even though I had put texts all over the place…(that’s what I’m guessin…) Since I had my stuff right where I wanted it (and de-parenting messed up the positions, and readjusting those was gonna take a while) I instantiated at the prefab’s transform.localPosition instead of position and they all came out great…

1 Like

Thanks for posting this! The last variant both solved a similar problem I was having and gave me deeper insight into how Unity works. The reason that it works and the explanation to why I keep running into strange scaling and positioning issues when instantiating prefabs is in the docs:

Thumbs up Senshi, that did it for me! I was putting true, but I needed false.

transform.SetParent(parent, false)

1 Like

Thanks Senshi!
i scratched my head for a while with this issue. It does make sense, but first i didn’t think the issue was in the parenting, i thought it was in some canvas component paramenter.

Thank you Senshi!
Passing the second parameter as false solved a similar problem I had! Now the children I instantiate a run-time scale with the parent that is added in the editor.

Thank you, that helped!

I was using SetParent, just not the false setting … sigh. Fixed everything. Thanks!