My Problem is prbably super simple but for my own good I want to break it down to part so I understand it.
So I have a bunch of images that I want to go into a prefab. Say it’s images of limbs to make out a full person.
So what’s the best way to put all that together in a prefab?
(I’ve had problems with sprites being invisible when I instantiate them)
So now I want to stuff all my different prefabs into a List, array or Dir (dunno which is best)
I have 27 different prefabs. I saw a video where he declared a GameObject array and then from the inspector he drag&drop filled it with the prefabs.
Now That I have my list of prefabs I want to instantiate them.
All I need to know is how to do one.
I tried Instantiate(prefabName[0], new Vector3(0,0,0), Quaternion.identity);
But I don’t know if it works because I can’t see anything.
I tried prefabName.transform.setParent ( to my canvas) but the array don’t seem to have the .transform available. So I’m lost.
If you can help me figure this out, I’d very much appreciate it.
If you look at the docs for Instantiate, you can include the parent transform in the method call.
Otherwise, what you can do is something like this :
GameObject myNewObject = Instantiate(myPrefab);
myNewObject.SetParent(canvasTransform); // there is an optional second parameter 'bool : world position stays' you can read about it in the docs
If u want to use any prefabs on canvas, better to use RectTransform. And u need to use canvas components, like Image and etc. But how i say before - Canvas not for characters, its for interfaces.
The best way to put it all in a prefab is making a script that can hold the sprites (a public Sprite[ ] limbs for example) and drag them from your asset folder to the inspector. If you somehow can’t drag & drop then I’ll say AssetBundles are the best way. (For now don’t mess with bundles, just keep in mind they exist).
You store in a List if you will need to add / remove stuff during gameplay.
You store in a Array if the amount of stuff is constant during gameplay.
And you use directories if you have a lot of stuff on the list but will only need a few and instead of loading everything and denting performance, you load only what you need, this would also involve bundles.
If “prefabName” is an array of game objects, then it will surely not have a transform, transform is a complex component of GameObject made by Unity while a array is just some generic storage method default of c#. Only game objects have a transform.
I believe you were trying to do this instead: prefabName[INDEX].transform.setParent(parentTransform)
This would compile but it’s not what you wanted, the instantiated game object is NOT on the the array, the array is merely holding a bunch of “blue prints” for the instantiate method to use.
Instead try what** methos5k** said, the “myPrefab” should be something like
Instantiate( prefabName[index] )
“myNewObject” is now a game object, and you should see in your Hierarchy a new game object named “[Name of your prefab] (Clone)”. Check the proprieties of this new clone and see if you can figure out why it’s “invisible”, few hints: a Sprite Renderer with no materials will not render and Images will not render if they are not the child of a canvas, and both of those could be behind other objects if their order in layer is lower. And remember to check their transform position.
I hope I manage to give you some insight, but take what I say with a pinch of salt. I’m not a master coder.
One thing to remember is that if you are using a prefab to instantiate a new game object, make sure you take the return value of the Instantiate method to use when setting your parent.
If you use the the prefab, not only will you get a warning/error, but it won’t do anything, because the prefab itself is an asset, not a scene object. This is a common mistake, and just something to remember
There is nothing wrong with using the canvas for your entire game if you wanted. It does have some things you have to consider if you go that route, but it’s perfectly capable of handling more than UI.
Now, that being said, the reason you might be careful of wanting to do that with a character is that canvas using the order of it’s children to determine what is in front of what, which is something you’d have to consider.
Also, your sprites are probably not invisible if you are using a sprite renderer. It’s probably just really small. When you add one, you should double click it to zoom in. For canvas objects, if you use the overlay mode, your image stuff will be huge, but that is how it’s designed. Overlay mode tends to make for better UI stuff as it will always be on top of stuff in your scene. This is usually something that throws new users off when they canvas is huge and they aren’t sure why.