- Home /
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 http://www.tallior.com/unity-scriptableobject-factory/
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
You can't reference $$anonymous$$onoBehaviours like that because they are components.
Could you elaborate a bit on what you are trying to accomplish with this?
A ScriptableObject can reference $$anonymous$$onoBehaviour script instances just fine. But not if they are inside a scene. This is a scope / extend / lifetime problem. Assets has a "longer" lifetime / greater scope as objects in a scene. Therefore scene objects can reference assets just fine but not the other way round.
Assets can reference other assets. So if you have a prefab with a $$anonymous$$onoBehaviour script attached, a ScriptableObject stored as asset can reference the $$anonymous$$onoBehaviour instance on the prefab since both are assets and don't belong to a scene.
Answer by Bunny83 · Sep 13, 2015 at 07:06 PM
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.
Answer by Salim · Dec 20, 2018 at 04:18 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612033555im_/https://answers.unity.com/themes/thub/images/avi.jpg)