What could prevent an object from being visible in a Release Build while being visible in debug mode?

Some context:
I’m using Unity Serializer to serialize a hierarchy of mesh in order to save it or send it to another app. I can save the meshes in a file in Project A and then load them up again, still in Project A. I can also transfer the data to Project B while it is running to have it display the meshes. Things are looking good and I can see the objects in Project B like I’m supposed to.

That is, everything works great while I’m in the Unity Editor. When I build Project B and launch the executable, I cannot see the objects in my scene. Of course, since it’s in Release mode, I cannot simply inspect the objects. What could make an object invisible only in release mode?

Here’s what I tried:

  • Through code, I can see that my gameobject does exist
  • That gameobject has a parent, the Hierarchy Root, which has no parent
  • Both object have normal Position, Rotation and Scale
  • If I spawn a primitive at my mesh’s position, the primitive will be displayed
  • Both the mesh and the root are activeInHierarchy==true, use layer 0 (default) and are untagged
  • The mesh has a renderer (CubeMesh) which is enabled, and a MeshFilter (CubeMesh)
  • Changing the renderer.material to a known good material has no effect
  • Cloning the gameobject will not display the clone
  • The mesh has for components: Transform, BoxCollider, MeshFilter, MeshRenderer and some components for the serialization
  • I can interact with the scene, it isn’t frozen

I’m a bit lost for words. Are there any other attributes which could hide an object?

The Serializer could be the problem, but loading the meshes from a file (same method, just different way to get the byte array) works just fine, even in a Release Build.

So after 6 hours of feeling like I was taking crazy pills, I figured it out.

One thing you must know is that Unity Serializer keeps the ids of all the prefabs you are using for your saved objects. When it is deserializing, it looks through the project directory to find these ids and the prefabs. But I was already aware of that fact and so I had copied all my prefabs from Project A in Project B and the deserialization was done just fine.

Here it gets interesting. As far as Unity was concerned, nobody was referencing the prefabs I had included in the project (Unity Serializer has no direct reference, only through an id). So when I was building the executable, the engine didn’t include the prefabs, which were considered useless. At runtime, Unity Serializer wasn’t able to find the right prefabs and so the objects were incomplete and somehow invisible. Putting in the scene an instance of every prefab forced the compiler to include them and it started working as intended.

Moral of the story: if you use indirect references to resources in your project, be sure that there is at least one direct reference to these resources, because in Editor, anything that is in your directory can be referenced, but in Release, only items that are referenced are included.