How to check if an object is asset in build runtime (Not in Editor) ?

Can I get some attention to this question please?

As far as I know the concept of a “prefab” doesn’t exist in a built runtime.

I’m confused by your use case anyway. When you say “scene objects” do you mean GameObjects in a particular scene? In all scenes? Why do you care if they were instantiated from a prefab or not? Are you trying to distinguish between objects created in code and objects that just exist in the scene when it’s loaded? What problem/odd behavior are you seeing in your library that you’re trying to resolve?

Prefab concept exists in build runtime in a different meaning. You may have a reference to a prefab object and you can instantiate it whenever you want. The object waiting to be instantiated can still be called as a prefab because it’s not attached to a scene yet.

  • Scene objects: all the objects which currently exist in all loaded scenes (loaded with the scene or instantiated programmatically).

  • Why do I care if they were instantiated from a prefab or not: Please see this section in my question “For those who want to know why I need this”.

*Are you trying to distinguish between objects created in code and objects that just exist in the scene when it’s loaded?: The answer is no. They are same for me. When you instantiate an object from a prefab, the new instance gets attached to the active scene automatically so it becomes a scene object too.

So serialized references to things that have no scene instances? A reference to something via Inspector doesn’t have to be a prefab and, likewise, a prefab can be reference by multiple other things that may instantiate instances of that prefab into the scene at different times.

I know. See my response

It seems like you want to do…something…for objects of a particular type in all scenes. What do prefabs have to do with any of that?

This is not about serialized objects. When you make a build, everything is serialized even the scenes and the objects in scenes.

Those serialized objects gets constructed and kept in memory when a scene loaded. This is also true for serialized prefabs. If you have a reference to a prefab in a scene, that prefab will be constructed and kept in memory too. If it wasn’t constructed on scene load, you wouldn’t be able to create an instance of it.

You can test this by implementing a constructor for a script which is attached to a prefab. That prefab must be referenced in the scene. If you log to a text file in the prefab script’s constructor, you will see that the script was constructed when the scene loaded in build runtime. You don’t even need to instantiate it.

In my class library, I have list of objects I need to process and that list comes from the programmer (who uses my class library). When I am processing these objects (sending them a message via a method call), I need to skip the prefabs (the objects which don’t belong to a scene). The list I need to process is coming from outside. I don’t have a control on the content of the list. The programmer can add a prefab reference to the list. I have to skip those prefab references when processing the list.

I know I make it sound complicated but actually it’s very simple need.

Can’t use use GameObject.scene? If the GameObject is part of a scene, scene.IsValid() returns true. If it’s a prefab that exists in the project, thus not part of a scene, scene.IsValid() returns false.

// Save as Assets/Editor/TestCode.cs
using UnityEngine;
using UnityEditor;

static class TestCode
{
    [MenuItem("Example/Print to Console")]
    static void DoMenuItem()
    {
        var obj = Selection.activeGameObject;
        Debug.LogFormat("Has valid scene: {0}", obj.scene.IsValid());
    }
}

Using the above code, select a GameObject or Prefab Instance in the Scene/Hierarchy and execute the “Example/Print to Console” button from the Unity main menu. It will output:
Has valid scene: True

Select a Prefab in the Project Window and click the button, it outputs:
Has valid scene: False

1 Like

@Peter77 This is the suggestion I get from people. I tried that too. In some cases (which I can’t describe yet) I saw that the gameObject.scene was empty even for scene objects. Maybe I need to give it another try.