0
\$\begingroup\$

I'm currently working on a 3D RPG game and I'm adding Interactions with objects. I have a lot of objects to interact with and I've placed them in an folder/empty object "Interactables".

Now, I want all my objects to have a SpriteRenderer Component. I'm currently adding this Component on each Interactable Object, one by one.

Is there a cleaner way of doing that, for example adding the Component to the Interactables parent empty object and cloning it to all the children ?

Thank you!

\$\endgroup\$
  • 1
    \$\begingroup\$ What about creating prefab? \$\endgroup\$ – Sergey Aug 20 at 10:55
  • 1
    \$\begingroup\$ You can add components to multiple objects at the same time. Just select these objects and add the component via the inspector. \$\endgroup\$ – Chillersanim Aug 20 at 11:09
  • \$\begingroup\$ Both these comments could make good answers with a little more step-by-step breakdown. \$\endgroup\$ – DMGregory Aug 20 at 11:37
1
\$\begingroup\$

The new Unity prefab workflow has a new feature called "prefab variants", which you can use to create prefab inheritance chains. You can create one prefab "Interactable Template", and then create multiple Interactable prefabs which are variants of that template prefab.

Each prefab variants can then:

  • Override certain properties of individual components (like the Sprite used by their SpriteRenderer)
  • Have additional components (like scripts which implement what happens when the player interacts with them)
  • Or even have additional child game-objects.

When you change the base prefab, those changes will be automatically applied to all the prefab variants, unless they have overridden that specific property. So if you add a SpriteRenderer component to the template then all the variants will also have a SpriteRenderer.

Unfortunately it is difficult to change to this architecture retroactively when you already created a bunch of similar but technically independent prefabs. So if you want to convert all the prefabs you already made to make use this new technique, you might have a bit of work ahead of you. But if you expect to make more changes to all your interactables in the future, then this might be a worthy investment of your time.

\$\endgroup\$
0
\$\begingroup\$

Unity allows you to add components to multiple objects at the same time with multiple selection. If they are in the scene and all of them are under the same empty parent object, you need to click parent and the last child object of the parent object while holding down the Shift to select all of the interactable game objects. While all of them are selected, you can add any component to them with the help of Inspector.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.