Object reference pointing a Scene NullReference on build

Hi everyone!!

I’ m just facing a strange problem, I have one Object variable in my code that represents a Scene so I can get the name of a scene in a more editor friendly way:

 [SerializeField]
  Object scene_data;

It works on editor, but when I’m on build the script loses the reference to the scene and is null.

Some ideas guys??
Thanks in advance.

The type for scenes (SceneAsset) is a UnityEditor type. This means that even if you’ve got the object declared as Object, it won’t get included in builds.

We just talked about how to solve this in another thread, there’s some code linked here that solves the problem.

Thanks Baste, I’m going to implement it in your way, hope it’s works !!

Edit: I have to do some workaround so I can use the class you developed, is there any benefit from use the path of the scene instead of use the name of the asset itself?

I understand that the callbacks from serialize are called before building the project so there I save the name of the asset itself:

public void OnBeforeSerialize ()
{
m_sceneName = AssetDatabase.GUIDToAssetPath(m_GUID);
actual_name = AssetDatabase.LoadAssetAtPath<SceneAsset>(m_sceneName).name;
}

In case it would be useful to some other people!!

Thanks agains Baste!