i want to create 100 images and text boxes in code in a for loop. how can i do this without add component, which is apparently obsolete and doesn’t work with canvases anyway?
what i mean is, do i have to add these in the editor? i don’t want to slow the editor down so it seems more efficient to do it in code.
AddComponent is not obsolete. The version of it that accepts a string is obsolete but you can still fully use either the generic version or the one that accepts a Type argument:
If you only need to cycle through 100 images / text, without them existing simultaneously, then you can just reference a single Text component and Image component and change the values.
If you need to create the images simultaneously, then then you can reference the root canvas (or a sub-object for organization), then create new GameObjects and AddComponent to that, re-parenting them to the root object so that they’re in the proper place in the hierarchy. This would work fine- you just need a new GameObject per each Text and Image you want to create, and AddComponent is an important part of that.
To make things slightly easier, you can make simple prefabs of GameObjects- one with a Text component, and the other with an Image component, and instantiate those directly. This way you don’t need to use AddComponent, and you can store other data that might be relevant like the layer and default dimensions easier.
is there a way to make sure the text and images are properly placed in the body of the scroll view? i’m thinking i could just do it in the editor, but i’m trying to do it the “proper” way so i learn coding - that’s part of the point of my project, to use it on my resume.
edit: okay, i think i’m just going to do it in the editor. it’s not that hard to do, and i have a pretty good laptop. i’m thinking i might try 50 items first. still if anyone knows how to format text/images in code, that’d be nice to know.
If it’s something in a scroll view that is “organized” for displaying, I agree that the option of a prefab (or 2) sounds best… this way, you can accommodate the layout you want or whatever, and simply instantiate your 50 or 100 items. Especially if each ‘pair’ of image and text is the same.
More details on your end goal exactly might get you some other options, too