Internal references are lost when I make a prefab

I’ve made an in-game editor that lets me build a spaceship out of parts. What I do is I build a ship with it then Edit->Copy it while the game is still running. Then, I end gameplay and paste it into one of my project’s scenes—it’s a great way to quickly make enemy ship “designs.”

The parts of these ships keep track of each other with public variables. These references are preserved when I copy/paste, but they all become null when I make a prefab out of the ship. What’s the deal with this? They are all internal references. In this case, simply making a prefab is changing the properties of my object.

:?: :?:

Prefabs can’t reference objects only found in a particular level (ie, they can only reference other prefabs). Perhaps this is the problem?

Could be…? I was under the impression that when making a prefab, it contained references to other objects within the prefab.

If I had a car GO with a steering wheel child that referenced other wheel children, I would think that the references would stay connected when the car is made into a prefab.

Something like:

MyCar
----Steering Wheel (script attached that turns wheels or something)
----Front Wheels
----Back Wheels

In my case, after making MyCar a prefab, I get null references when the steering wheel tries to turn the tires, because it can’t find them anymore.

It looks like if I set references in the inspector, they stick around after the prefab is made, but if they are set in a script (like in my ingame editor) they are lost.

Kind of defeats its purpose. :?

I’m going to go ahead and respond to my own post again to add that I’m getting this error when making prefabs of objects while the game is running:

UnityException: The component is not attached to any game object!

Again, these are objects put together with code, not in the editor. I can only guess that creating a prefab duplicates an entire hierarchy, somehow orphaning the originals. However, using Edit->Copy seems to perfectly preserve all relationships.

I’m still stuck, though. How can I move these objects from the hierarchy to the project view? Some form of complicated serialization?

This makes absolutely no sense, how else can you take Game Objects from one project to another. If they have the same dependencies in the project hierarchy I don’t see why the linkage wouldn’t remain. I even tried putting the prefab into a .unitypackage with all of the dependencies.

by exporting packages.

prefabs are blueprints, blueprints logically can only contain other blueprints.

you don’t expect a blueprint of a car to contain a real tire, you expect it to contain a blueprint of a tire … think the same way when you think about prefabs and “logic”

or put differently: a prefab can not contain any object thats not a prefab as the object would not work when the prefab is instantiated in any other scene. The prefab can only instantiate stuff thats present in all scenes → prefab

I did. What I was trying to say is I exported the prefab as a package with all of its dependencies from one project, and expected to be able to import it into another. When that happened, the hierarchy was completely broken and all of my scripts were broken mono behaviors.

worked for me in the past, so I would have to guess that you transported it from unity to unity iphone or vice versa with incompatible versions.

either that or you just selected the object and export instead of selecting it and exporting it with all dependencies though it sounds like you did with dependencies and with them selected on the export (as well as selected on the import).

what naturally is a requirement is that you don’t have scripts - objects with the same name in your project that are in a different place. then the import will not work and thus all references are broken.