Script and scriptable objects show different behaviour on this.

Hi,
I created a script with a serialized field in where I dragged and dropped a gameobject and I’d get its rect transform, like this:[SerializeField] private RectTransform parentPanel = null;
For sure it works


The problem I got is when I try to do the very same thing with scriptable objects. The code is exactly the same, however I simply cannot select any gameobject (Editor “Parent Panel” field is None, no gameobjects are nor selectable neither dropable).
What am I exactly doing wrong? What am I missing?

You cannot reference objects in a scene from an asset (like a ScriptableObject saved in the Assets folder).

This is both a restriction in the serialization system (it doesn’t know how to reference parts of a scene from outside that scene), and a logical restriction (what would it mean to try to access an object in a scene when that scene isn’t open).

What are you trying to do?

I have a set of scriptable objects called gameState that update some elements within a scene. I want them to dynamically generate/destroy several gameobjects (UI related) whenever a the current gamestate changes, and so I wanted to provide the position where they shall be created at.

@Ifridos as Baste said: Simply change the direction of the dependency. Reference the prefab instead of the gameObject in the scene and instantiate whatever you have referenced. That works.

@MNNoxMortem I tried that too, still not working.

@Ifridos , We use that widly throughout the project. The ScriptableObject provides the references to the prefabs, the instantiation object gets a reference to the scriptable object.

4131823--363436--upload_2019-1-22_13-58-40.png
Drag the prefab from the project view directly on your public or [SerializeField] GameObject (or Component Type) reference. That definetly works.

1 Like

@MNNoxMortem I know, I’ve used it in the past too, but it’s not working right now. I thought I could be missing something, but…

Drag it… not search it.

If you want to search it - define it as gameobject.

I still couldn’t drag&drop it until after restarting the computer, not even restarting the editor would do it (which is what I tried before opening this thread).
Thank you all.

@Ifridos Great that it works now :slight_smile: