2
\$\begingroup\$

I have a concern about prefab's purpose.

When I create a npc like a monster with all it's animations, script etc, I then create a prefab to instantiate it.
When I want to insert different monster which are quite similar to the first one I created I wonder to understand what's the Unity base idea to deal with. My thought is that the prefabs should be kept the minimum essential numer possible and to call different functions to initialize time by time differently depending on specific needs.
But, how to deal with animators and animations in general? There is any shortcut of creating one prefab for each monster? I'm asking because I can't find any clear way to dinamically attach animators to a generic gameObject. Since I cannot have one Animator where I replace dinamically sprites involved the simplest (but not so smart) way I found is to create duplicates of prefab and attach editor side all these graphic components.

What would be the best approach to deal with this?

\$\endgroup\$
1
  • \$\begingroup\$ How similar do you mean by "quite similar". Do they have exact same features and scripts? There is a video by Unity where the presenter has a script that changes only the sprite at runtime. Are you looking for something similar? Link to video: youtube.com/watch?v=rMCLWt1DuqI \$\endgroup\$
    – SanSolo
    Commented Oct 25, 2015 at 14:08

1 Answer 1

1
\$\begingroup\$

Prefabs work best when there will be multiple instances of any particular structure across your game. For example a Red Monster might have a red texture and do 10 damage to player via a monsterDamage variable (publicly exposed on a script). Another monster might be Green Monster, and it does 20 damage to the player, and also uses the same script as the red monster but with the monsterDamage modified via the inspector.

Prefabbing both of these means that whenever you want these monsters with their scripts attached in a scene you simply drag them in and they are good to go with the correct values set on their parameters and the correct components attached (such as scripts, Colliders etc).

Another really great benefit is that if you decide that for example that Green Monsters are doing too much damage, you can select the prefab in your folder and change this value to say 15. This will update any green monsters in your game to now do 15 damage instead of 20!

TLDR;

Prefabs reduce the need for repetitive work and allow you to make a change to an object which will be applied to all instances. Instead of going through every instance manually.

If you place an animator on a monster prefab then the animator will automatically be prefilled for all instances of that monster

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .