Scriptable Object Cannot Reference Another Script In Scene

Hi,

I’ve started experimenting with ScriptableObjects. For some reason I cannot reference other scripts in the scene from the ScriptableObject.

So for example I have generated and saved a ScriptableObject using Loading...

Now that I have the ScriptableObject I try to reference another script either by dragging (nothing happens) or selecting (nothing available to select).

I have looked for solutions but they all seem to be about referencing a ScriptableObject from other scripts.

Here are some example scripts I put together.

using UnityEngine;

public class SOTesting : ScriptableObject
{
    public SomeOtherScript someOtherScript;
}

And attached to a GameObject in the scene:

using UnityEngine;

public class SomeOtherScript : MonoBehaviour
{
    // Does stuff
}

I hope this makes sense! By the way I am using the ScriptableObject to try and save data that persists during and after runtime.

Thanks.

Paul

The “scriptable object factory” that you’re using seems to create instances of ScriptableObjects and store them as assets in your project. No asset can ever reference an instance in a scene, no matter if it’s a prefab or a scriptableobject. A scene is saved seperately as asset and it’s content is only available when the scene is loaded while assets like prefabs and scriptableobjects are always available.

How should a scriptableobject that is stored as asset in your project reference an object in a scene that might not be loaded at all?

Objects in scenes can reference assets without problems since assets are always available. Things that are located in the same scene can also reference each other since they are all available when the scene is loaded. However assets can’t reference objects that are located in a scene. Just like an object from scene “A” can’t reference an object in scene “B”.

Scriptable objects are ment to be not bounded to a scene at all. If your class need access to things in the current scene then it should either:

  • be in that scene as well (as MonoBehaviour and not as ScriptableObject)
  • use some code that “finds” the instance dynamically at runtime and setup the required connection.

You can have the GameObject or MonoBehavior in the scene "register’’ itself to a ScriptableObject asset on Awake().

public class TheMonoBehaviorClass : MonoBehaviour
{
        public TheScriptableObjectClass myScriptableObject;
        
        void Awake()
        {
        		//Register to scriptable objets
        		myScriptableObject.RegisteredMonoBehavior = this;
        }
}

The value indicates a type mismatch in the inspector of the ScriptableObject but the reference still works.