A Question about Prefabs

So, my question is should I really create multiple prefabs for every object?

To give more context, let’s say I am creating Cards.

Well, let’s say there are 300 cards, should I really create 300 prefabs? One for each Card?

Is there a better way to create maybe only a single prefab of a Card, and then when instantiating it, just sets its Sprite, health, attack, resource cost, etc to different values?

That way I don’t have to deal with 300+ prefabs?

Any help would be appreciated.

If you have 300 prefabs of anything, you’re doing something wrong.

It’s definitely better to have a script on a card that can set its textures, health, resources etc… that way you have 1 card and 1 script that manages all that.

1 Like

Alright, that leads me to my second question I suppose.

I know how to programmatically get the SpriteRenderer of an object and set its current Texture.
But, I can’t seem to have a TextMesh and a SpriteRenderer on the same object.
How would I go about displaying a Sprite, and 3+ pieces of text on the same game object?

Aha, I think I see a misunderstanding. What DroidifyDevs is saying is you create one card prefab - a card object with all the children needed to create its look. That’s how Unity is designed, it’s not really designed to have one GameObject with multiple renderers. When you take that card (with its children) and drag it from the scene into the project, that turns it into a Prefab, and DroidifyDevs is saying it’s not a good idea to create each of the 300 different card types and turn each one into a unique prefab already setup; rather have just the one and change all the bits programmatically as it is spawned.

So for example, after spawning a card, get the child with the name and change the text, get the child with the image and change the sprite texture, etc.

1 Like

Yes, I understand that completely.

My question evolved into more how do I display text and a Sprite on a game object?

You can’t, Unity is designed to only have one renderer per GameObject. The only way I can think of doing such a thing is to use a RenderTexture, manually compositing a single texture from the text and sprite image yourself. Or you could create a shader that mixes two textures.

You can put a World Space Canvas for text and sprites (or Images in the same World Space Canvas) on the object.

1 Like

That seems really cancerous for such a simple idea.

I just told you, have an empty parent object with a script on it, and as a child put on a world canvas. In that world space canvas you can set text, images etc… you can put an image on the world space canvas or make another child sprite object and change its texture. Not too hard, definitely don’t need a custom shader for that.

1 Like

Okay, thank you.

I will try that out and see how it works.

Great! If you run into problems, let me know.

So, I have been busy and haven’t gotten a chance to try out much what you said.

Edit: I added a “Canvas” Component, and then made its type “World Space”. Ignore the stuff below.

But, in the Inspect on my Empty GameObject, I press “Add Component”, and then I search for “World…”

And my only results are:

“World Anchor”
“World Particle Collider (Legacy)”.

Sorry if this is a dumb question, maybe I should just be pointed towards (or find my own) tutorial for World Space Canvases.

Edit: Now that I have my World Space Canvas added I am a bit confused on how to get text and an image in there, but I will keep playing around with it.

I am sorry I don’t want you to spoon feed me or babysit me.

But, like I said above, I am trying to create cards with Names, Descriptions, Health, Cost, and Attack, as well as a Sprite for each one.

I added a Sprite Renderer to my Canvas (I think), and a Text UI Component. But I can’t seem to position my Text Element, and I can’t add multiple text elements.

Don’t add a sprite renderer. Add either an Image or RawImage to the canvas (i.e. right-click on game object → UI → Image). This is UI stuff, so you’ll want to stick with UI components.

Do you know how the Canvas system works?

Perhaps try this: https://unity3d.com/learn/tutorials/topics/user-interface-ui

In order to work with the Canvas UI system you need to fully understand how it works. It’ll be a lot easier as you’ll know what to do.

I’ll take a read, and just to be clear: this is for a card that would be on a board (im not sure if that falls under the UI category). Thank you for your help

That would fall under a World Space canvas. A World Space canvas can be put anywhere in the scene, so it could work.