- Home /
Why is it that I can make prefabs (which are assets) reference scene objects but not be able to do the same for ScriptableObjects?
According to Bunny here, ScriptableObjects are assets, assets can only reference other assets and not scene objects. Scene objects can reference other scene objects and assets alike. Question is, aren't prefabs considered to be assets as well? If so, why is it that I can reference scene objects from prefabs? i.e.
public class TestSO : ScriptableObject
{
public Object obj;
}
public class TestBeh : MonoBehaviour
{
public Object obj;
}
Creating an asset of TestSO and trying to drag-drop a scene object to 'obj', Unity won't allow me. Creating a prefab of TestBeh and doing the same, I can assign 'obj'! Why?
Can someone elaborate on this a bit?
Thanks.
Edit: It was a mistake as I was 'user the impression' that I was using a MonoBehaviour when I was able to assign scene objects to prefabs, but I was not, I was using a custom BetterBehaviour. You definitely can't and shouldn't assign scene objects to prefabs. End of story.
Answer by troien · Dec 03, 2014 at 11:15 AM
Well, you can't, and you shouldn't be able to. (When I try to do that, I can't at least).
But maybe you misunderstood something.
Because 'Scene Objects' are objects that are used in the scene and are shown in the hierarchy of a scene. Assets are things that reside inside the asset folder, in Unity they are visible inside the Project window. Any prefab in the project window can't reference scene objects (from the hierarchy) but can reference other assets (From the project window).
The reason for this is simple, There can be more then one scene. If you would have a reference to a object in the scene, and then try to instantiate the prefab in another scene, the reference would be broken as the thing that you are referencing doesn't exist inside the other scene. But assets can still reference other assets
What you can do however is instantiate a prefab (when you drag a prefab from the project folder to the hierarchy) and then drag a scene object into one of the fields of the instantiated prefab. Because Instantiated prefabs are scene objects, with a link to an asset. They are not assets themselves, only the prefab in the Project window is an asset.
Thanks. This makes sense, but this is why I asked the question. I 'can' drag scene references to prefabs, unity won't disallow me. Going into a different scene however and checking the prefab's values that it got from the other scene, they're now null (makes sense). Unless we're missing something, this could be a bug where someone forgot to put in false in ObjectFields' 'allowSceneObjects' flag
Are you using a custom inspector of some sort. Which uses an ObjectField with allowSceneObjects set to true?
Otherwise I indeed think this might be a bug ye... When I try your setup I'm not able to assign any scene objcet to that field if it is a prefab.
Yes Lol my bad. I forgot that I was using BetterBehaviour (from VFW) ins$$anonymous$$d of $$anonymous$$onoBehaviour.
Ok,
For people looking how to fix this. If you have access to the Editor script, you might want to set the allowSceneObjects of any/all the ObjectField's to '!EditorUtility.IsPersistent(target)' as seen in this example. That way you can't add Scene Objects to it when it is an asset. But can when it is a Scene Object.
Or use !AssetDatabase.Contains(target)
- Not sure if there's a difference, is there?
Your answer
Follow this Question
Related Questions
Saving changes to the asset files for Prefab and Scriptable objects. 1 Answer
Add a GameObject as a sub-asset of a ScriptableObject 1 Answer
ScriptableObject assets null on startup, then reappear? 1 Answer
Getting a prefab with javascript FindAsset (Solved) 0 Answers
Creating prefab from instance of a GameObject loses materials 1 Answer