Creating a generic sort of Camera.CopyFrom?

What I want to do is take a script attached to one game object, completely duplicate it, and attach it to another game object. The problem is, I don’t know ahead of time what kind of script this is going to be, and due to the issues I was having in this thread, I’m not sure what my solution could be.

I though maybe EditorUtility.CloneComponent could help me, but I don’t think that’s appropriate. Please let me know what that method would be used for, anyway.

GameObject.AddComponent only can attach types of components, not actual instances, apparently. So I don’t think that’s going to help me.

The documentation for CloneComponent is a bit sparse, isn’t it? But the way I would interpret it, it should do what you want here - take an existing component and duplicate all its values.

The problem is, it attaches th cloned component onto the same game object. I need the component to move freely, onto other game objects.

Jesse,

Can you tell us more about what problem you are trying to solve? If we know why you want to do this, we can possibly come up with some suggestions.

My attempt at humour:

Bob> Can you tell me where I can buy a motorcycle helmet from?

Jim> Yeah, sure, there are lots of shops that sell motorbikes and accessories. Why do you want a motorcycle helmet?

Bob> Well, I need to travel from London to South Africa. I saw this TV show about a couple of guys who drove motor bikes all the way down through Africa, so figured I’d do the same.

Jim> You are going to drive a motorbike from London to South Africa but don’t know where to buy a helmet? You are crazy, why not fly there?

Bob> Flying? Oh yeah, I never thought about that. That’s what I’ll do.

The answer you get depends on your question.

-G

I use Blender as a “level editor” for Unity. In Blender, I use one .blend file as a level, and for anything that I am going to reuse, I link in from another .blend file. That way, I can make changes to the original, and have it propagate to all the others. Essentially, the only thing I am doing to the reused assets, in the “level”, is object-level transforms.

So for anything that gets completely reused, automatically putting a Unity prefab there is a perfect solution. I basically wipe the hierarchy clean for a Game Object (which represents the Blender file), and rebuild it, replacing Blender meshes with prefabs, based on names.

However, when creating these prefabs, and again, syncing to a Blender file, it’s obviously nice to not have to reattach all the components every time I perform a sync. What I wanted to do, was to see what scripts were attached to a game object, make exact copies of those, with all serialized variables, and attach them to all the Blender meshes that contained a specified name.

What I actually decided is probably more useful, and what I have modified the scripts to do now, is to simply not delete the original game object, but have its transform adjusted to match the Blender file. Because the actual mesh is updated, in the Blender file, this seemed to yield identical results.