Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

A Question about Prefabs

Discussion in 'Editor & General Support' started by DylanHolmes, May 19, 2017.

  1. DylanHolmes

    DylanHolmes

    Joined:
    May 19, 2017
    Posts:
    9
    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.
     
    Last edited: May 19, 2017
  2. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    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.
     
    aer0ace likes this.
  3. DylanHolmes

    DylanHolmes

    Joined:
    May 19, 2017
    Posts:
    9
    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?
     
  4. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    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.
     
    DroidifyDevs likes this.
  5. DylanHolmes

    DylanHolmes

    Joined:
    May 19, 2017
    Posts:
    9
    Yes, I understand that completely.

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

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    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.
     
    Last edited: May 21, 2017
  7. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    You can put a World Space Canvas for text and sprites (or Images in the same World Space Canvas) on the object.
     
    DylanHolmes likes this.
  8. DylanHolmes

    DylanHolmes

    Joined:
    May 19, 2017
    Posts:
    9
    That seems really cancerous for such a simple idea.
     
  9. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    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.
     
    DylanHolmes likes this.
  10. DylanHolmes

    DylanHolmes

    Joined:
    May 19, 2017
    Posts:
    9
    Okay, thank you.

    I will try that out and see how it works.
     
  11. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Great! If you run into problems, let me know.
     
  12. DylanHolmes

    DylanHolmes

    Joined:
    May 19, 2017
    Posts:
    9
    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.
     
  13. DylanHolmes

    DylanHolmes

    Joined:
    May 19, 2017
    Posts:
    9

    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.
     
  14. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    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.
     
  15. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    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.
     
  16. DylanHolmes

    DylanHolmes

    Joined:
    May 19, 2017
    Posts:
    9
    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
     
  17. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    That would fall under a World Space canvas. A World Space canvas can be put anywhere in the scene, so it could work.