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 /
  • Help Room /
avatar image
0
Question by Soraphis · Nov 10, 2015 at 04:54 PM · scriptableobjectassetdatabasenested

Get asset file from scriptableobject (Nested Scriptableobjects)

Edit 2:

Ok i think i found the issue: When OnEnable is called the actual asset isnt created, it is created AFTER OnEnable. So you cant nest Scriptable Objects into a scriptable after creating an container asset :/

EDIT1:

Is it possible to get the asset file of an scriptable object in its OnEnable method?

ORIGINAL:

Hi, im playing around with Scriptable Objects. But I'm running into multiple errors. Most of the code and examples i can find only are 2+ years old.

I was trying some basic thing, basicly a linked list: I have an mono behaviour holding a reference on a Container, which has a reference on a child (and each child has a child reference too, ...)


1st Try

 [Serializable][CreateAssetMenu]
 public class Container : ScriptableObject{
     [SerializeField] public Child root; // Child derives from ScriptableObject
     public void OnEnable() {
         if (root == null) {
             root = CreateInstance<Child>();
 }   }   }

At this point i've run into my first error: type missmatch . Well I see why i happens: The Child instance is created but isn't saved anywhere (Maybe someone can explain it better than me).


2nd Try

But i want the childs to live inside this container, and not everyone getting an own file.

 [Serializable][CreateAssetMenu]
 public class Container : ScriptableObject{
     [SerializeField] public Child root; // Child derives from ScriptableObject
     public void OnEnable() {
         if (root == null) {
             root = CreateInstance<Child>();
             AssetDatabase.AddObjectToAsset(root, this);
             AssetDatabase.SaveAssets();
 }   }   }

Now I've got the error:

AddAssetToSameFile failed because the other asset is not persistent

I've googled for that error, but this error seems to be rare :/


So, how would one do something like that?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Baste · Nov 11, 2015 at 02:38 PM

Scriptable objects needs to be saved to file to be persistent. You do this through AssetDatabase.CreateAsset. So you want:

 if (root == null) {
     root = CreateInstance<Child>();
     AssetDatabase.CreateAsset(child, "Path_To_Object");
     ... //as before
 }
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 Soraphis · Nov 12, 2015 at 03:01 PM 0
Share

what is child? did you mean root? If so: this solution does not work.

To create the container as asset file i use the [CreateAsset$$anonymous$$enu] attribute. Why do i need CreateAsset? the asset file exists, which is the reason OnEnable is called.

and i want to add the root object to this existing asset file. Based on the documention it should not be necessary to create the subasset with createAsset

avatar image
0

Answer by tbriley · Sep 17, 2017 at 08:52 AM

late to the party but i had the same issue and there is a relatively simple solution - search for the path of your object and check whether it's null or empty:

 [Serializable][CreateAssetMenu]
 public class Container : ScriptableObject
 {
     [SerializeField]
     public Child root;
 
     public void OnEnable() 
     {
         var path = AssetDatabase.GetAssetPath(this);
         if (string.IsNullOrEmpty(path))
         { return; }
 
         if (root == null) 
         {
             root = CreateInstance<Child>();
             AssetDatabase.AddObjectToAsset(root, this);
             AssetDatabase.SaveAssets();
         }
     }
 }
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

37 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ScriptableObject not saving to Asset properly 0 Answers

LoadAssetAtPath doesn't seem to work for ScriptableObject asset in AssetPostProcessor 1 Answer

How can I get a sub asset? 1 Answer

ScriptableObject Instances Referencing One Set of Prefabs - Changing One SO Changes The same prefab 1 Answer

How to "Lock" scriptable objects from losing their [SerializeField] values 0 Answers


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