Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by code-blep · Sep 13, 2015 at 11:54 AM · scriptableobjectpersistent

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Serdan · Sep 13, 2015 at 06:07 PM 0
Share

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?

avatar image Bunny83 Serdan · Sep 16, 2015 at 10:22 AM 1
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image code-blep · Sep 13, 2015 at 08:07 PM 0
Share

Ahh! Now I get it! Thanks for the great explanation.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to serialize a reference to a ScriptableObject asset file using the JsonSerializer 2 Answers

Item database with override functions 0 Answers

Scriptable Object references to prefabs memory load? 1 Answer

Get Reliable Instance ID 0 Answers

scriptable objects slow down the editor 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges