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
0
Question by felix330 · Nov 04, 2018 at 12:15 PM · editor-scriptingscriptableobjectassetdatabase

ScriptableObject List variable doesn't save if created through script, works fine if created through Inspector

If have an Event class that is a scriptable object which is saved in the Assets folder. The Event has a list of a different, serializable class, EventProperty as a variable. Now there are two ways to create an Event: Using Right-click -> Create or through a script. Using right click works fine and it saves the EventProperties correctly. However, if I use the script, it seems to work fine at first, but once I restart Unity the Evenproperties List is empty. Other Variables, like the Event name are saved fine however. I've seen some similar questions but none of the suggested solutions worked. Here are the relevant bits of my code, maybe I'm missing something.

First, the Event class:

 [CreateAssetMenu(menuName = "VR Scientific Toolkit/STKEvent")]
 public class STKEvent : ScriptableObject
 {
     [SerializeField]
     public List<EventParameter> parameters = new List<EventParameter>();
     public string eventName;
     public Hashtable objects = new Hashtable();
     public int uniqueID; //TODO
     public float time;
 
     public void AddParameter(string name, int typeIndex)
     {
         AssetDatabase.Refresh();
         Undo.RecordObject(this, "Add Parameter");
         EventParameter newParameter = new EventParameter();
         newParameter.name = name;
         newParameter.hideFromInspector = true;
         newParameter.typeIndex = typeIndex;
         parameters.Add(newParameter);
         AssetDatabase.SaveAssets();
     }
 
 }

A part of the Editorscript where the Event is created:

 private void CreateEvent()
     {
         //Create Event itself
         STKEvent newEvent = (STKEvent)ScriptableObject.CreateInstance("STKEvent");
         newEvent.eventName = "SampleName";
 
        // [...]
 
         AssetDatabase.CreateAsset(newEvent, "Assets/VRScientificToolkit/Events/Track"+"sample.asset");
         Undo.RecordObject(newEvent, "Created Event");
         AssetDatabase.Refresh();
         //Create Eventsender
         STKEventSender s = trackedObject.AddComponent<STKEventSender>();
         s.eventBase = (STKEvent)AssetDatabase.LoadAssetAtPath("sample.asset",typeof(STKEvent));
         s.SetTrackedVar(trackedComponents,trackedVariables,savedNames);
         AssetDatabase.SaveAssets();
     }


And lastly the Eventsender function where the Parameters are set:

 public void SetTrackedVar(bool[] comps, bool[][] vars, List<string> eventVarNames)
     {
         //[...]
         
         for (int i = 0; i<comps.Length; i++)
         {
             if (comps[i])
             {
                 AssetDatabase.Refresh();
                 Undo.RecordObject(eventBase, "Created Event");
 
                 for (int j = 0; j<vars[i].Length; j++)
                 {
                     if (vars[i][j])
                     {
                         if (j >= trackedComponents[trackedCompsIndex].GetType().GetProperties().Length)
                         {
                             eventBase.AddParameter("SampleName",1);
                         } else
                         {
                             eventBase.AddParameter("SampleName",1);
                         }
                         varNameIndex++;
                         eventVariableIndex++;
                     }
                 }
 
                 AssetDatabase.SaveAssets();
 
                 trackedCompsIndex++;
             }
         }
     }

As you can see, what I basically tried so far is putting Undo.RecordObject() and AssetDatabase.SaveAssets() everywhere something is changed about the object, but no luck. I really don't know what to do anymore, it's just weird considering the other variables are saved just fine.

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
Best Answer

Answer by felix330 · Nov 05, 2018 at 01:13 PM

I figured it out! I had to set all the variables before creating the asset. Only had to adjust the order of commands in the CreateEvent() Function a bit, so now it looks like this:

 STKEventSender s = trackedObject.AddComponent<STKEventSender>();
         s.eventBase = newEvent;
         s.SetTrackedVar(trackedComponents, trackedVariables, savedNames);
         AssetDatabase.CreateAsset(newEvent, "Assets/VRScientificToolkit/Events/Track"+trackedObject.gameObject.name+trackedObject.gameObject.GetInstanceID().ToString()+".asset");
         Undo.RecordObject(newEvent, "Created Event");
         AssetDatabase.Refresh();
         s.eventBase = (STKEvent)AssetDatabase.LoadAssetAtPath("Assets/VRScientificToolkit/Events/Track" + trackedObject.gameObject.name + trackedObject.gameObject.GetInstanceID().ToString() + ".asset", typeof(STKEvent));
         AssetDatabase.SaveAssets();
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 hectorux · Nov 05, 2018 at 03:01 PM 0
Share

I think you can, also, check if something is null when storing it, and if it is null then initializing it

avatar image
1

Answer by hectorux · Nov 04, 2018 at 03:29 PM

[SerializeField] makes serializable just the below property, if you want it to more variables, then you have to put one over every one

Comment
Add comment · Show 5 · 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 felix330 · Nov 04, 2018 at 03:55 PM 0
Share

Thanks for the answer, the variable right below the [SerializeField] is the one that doesn't save though, so that can't be the problem.

avatar image hectorux felix330 · Nov 04, 2018 at 03:57 PM 1
Share

Do you know if event parameter is serializable?

avatar image felix330 hectorux · Nov 04, 2018 at 04:05 PM 0
Share

Yes, this is the entire EvenParameter class:

 [System.Serializable]
 public class EventParameter
 {
     public string name;
     public System.Type systemType;
     public int typeIndex;
     public bool hideFromInspector;
 
     public void SetTypeFromIndex()
     {
         systemType = ST$$anonymous$$EventTypeChecker.allowedTypes[typeIndex];
     }
 }

The weird thing is, it seems to serialize just fine, but only when it's created through the Unity inspector and not through my script.

Show more comments

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

107 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 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 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

Serialization issue with ScriptableObject 0 Answers

How to create custom asset icons for ScriptableObject instances 2 Answers

Converting XML-files into assets 1 Answer

ObjectField Get object type(Asset, Scene Object) 2 Answers

How to control class variables from an Editor Tool or external file? 2 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