How to store assets in game objects?

Hi,

this is a general (beginners) question, which I ilustrate with my current example: I created a game object which is mainly a camera and it makes use of a render texture and a material. The render texture and the material are both stored in the Assets folder of my project and they are referenced by the camera game object in the Unity editor.

Now I want to duplicate my camera game object several times and I want to use a separate (new) render texture and material for each of the cameras. However, when I just duplicate the game object, the same render texture and material is always referenced, so I need to create new ones in the Asset folder and change the references in the editor.
What is the best way to avoid this?
My idea was to create components in my game object: material and render texture (like e.g. a rigid body) but this is not possible. What else can I do? Is creating the render texture and material in code of each game object the best solution? Only downside is that I find it easier to change properties in the editor instead of the code…

Some best practises would be highly appreciated :slight_smile:

Best regards
Marko

Usually, assets in Unity are shared stuff. This behavior is expected and logical.

You can have on Game Objects more than 2 shared stuff as in your example. How does Unity understand which objects should be duplicated in your case? So there could be a dialog for this task with selecting the target stuff, but I didn’t see that.

1 Like

It would require editor scripting. You can have non-asset Unity objects that are serialised into prefab components or scene components (or into any asset, really), though there’s no real built in editor mechanism for doing this. Would need to make your own mechanism of sorts, such as an editor window, custom inspector, or a UnityEditor.MenuItem.

In this case I’d probably use the latter to generate a new camera prefab, with non-asset render textures and materials embedded into it. Though this is very much not beginner territory.

1 Like

Thank you both. OK, so there seems to be no simple solution. I might look into the given solutions at a later stage, good to know! For now seems easiest to duplicate the assets :slight_smile:
Best regards!

2 Likes