Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Nemox · Feb 14, 2015 at 02:41 AM · instantiateobjectserializationserializedpropertyserializable

Instantiated objects into serialized property?

So, I have a custom object. Let's call it Template. Within the Template, I have a list containing Actions. I have a few derived Action classes, let's say they're called Celebrate, Party, and Dance.

I've got my list of Actions set as a SerializedProperty, and I can select which derived class I want to use with a few buttons I've made. The problem is, I can't figure out how to instantiate an Action object and add it to the list.

Anyone have any insight?

Also note, I do have to rely on inheritance for Actions, though I'm not picky about the root type that the Action class derives from.

Here's one of a few things I've tried. Debugging shows that the array slot is being filled with something so it's no longer null, but I can't access any variables whatsoever, and using oAction.ToString () yields a blank string:

 SerializedProperty oActionList = serializedObject.FindPropertyRelative ("actions");
 
             Action oNewAction = Object.Instantiate (actions[(int)oAction]) as Action;
 
             oActionList.arraySize++;
             oActionList.GetArrayElementAtIndex
                 (oActionList.arraySize-1).objectReferenceValue = oNewAction;
             serializedObject.ApplyModifiedProperties ();

Comment
Add comment · Show 4
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 giulio-pierucci · Feb 14, 2015 at 02:44 AM 0
Share

can you post some code or a package?

avatar image Nemox · Feb 14, 2015 at 02:46 AM 0
Share

Hm... I'll see if I can isolate the relevant lines. The rest of it is a bit of a mess.

avatar image giulio-pierucci · Feb 14, 2015 at 02:49 AM 0
Share

It's enought 3 example classes

avatar image creeperman · Feb 14, 2015 at 04:36 AM 0
Share

INCO$$anonymous$$ING NEWS THEY DONT CARE (if you bought the game if not they will say something along the lines of...)

Hi that must suck, dont know why, how bout u go buy my game an get me a raise

2 Replies

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

Answer by Nemox · Feb 15, 2015 at 05:00 AM

Figured it out! Turns out to add the Action into the serialized array, it had to be in the form of a ScriptableObject or else it could not be of a derived class.

Then, because it was temporary data, it merely disappeared when the function ended and was cleaned up.

Instead, I had to add the Action to the Template's asset file, then set the array slot's reference ID to the new action's ID, in that order.

 AssetDatabase.AddObjectToAsset (oNewAction, Selection.activeObject);
 
             oActionList.arraySize++;
             oActionList.GetArrayElementAtIndex
                 (oActionList.arraySize-1).objectReferenceInstanceIDValue = oNewAction.GetInstanceID ();
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
avatar image
1

Answer by nathanielstevens · Feb 14, 2015 at 05:58 AM

It's a little hard to tell with your snippet of code, but my first guess would be to look into having your Action class inherit from ScriptableObject and then you would use CreateInstance(). You can also you use the [System.Serializable] attribute on the Action class and just use the 'new' keyword (depending on what kind of data you're trying to serialize). I won't go into any more detail in case you've already checked out the avenues but please let me know if you need more information or I'm taking you down a path you've already been on.

http://docs.unity3d.com/ScriptReference/ScriptableObject.CreateInstance.html http://docs.unity3d.com/ScriptReference/Serializable.html

Comment
Add comment · Show 7 · 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 Nemox · Feb 14, 2015 at 04:27 PM 0
Share

Tried it with CreateInstance as well. No matter what form the object is in, it doesn't seem to be getting saved.

I've debugged it a little, and it seems that the serialized Template's action array items are not null after all, but they still contain no data whatsoever. I can't access any variables, and .ToString() yields nothing.

avatar image nathanielstevens · Feb 14, 2015 at 06:17 PM 0
Share

Could you post the code for the Action class? It may have to do with the data types it's using (This serialization stuff can be a real bear. I've spent the last week struggling with it myself).

avatar image Nemox · Feb 14, 2015 at 06:49 PM 0
Share
 [System.Serializable]
     public class Action : ScriptableObject {
         public string testString = "Test.";
     }


The derived classes have no variables of their own yet.

avatar image nathanielstevens · Feb 14, 2015 at 10:26 PM 0
Share

What does Template inherit from? And when you say you can't access any variables, do you mean it's returning null or that there's some sort of error?

avatar image Nemox · Feb 14, 2015 at 10:58 PM 0
Share

Template is just a ScriptableObject. When I go back and serialize the list of actions, I check whether each action == null. The test shows that they aren't null. But when I try to access anything like action.testString, it gives me a null reference exception error.

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

22 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

Related Questions

Instantiating a serializable class and variable 1 Answer

Getting any type in Unity Inspector 1 Answer

Error when trying to Serialize a field that is in a class 0 Answers

Calling 2 specific random float numbers in random.range using array not working! 2 Answers

Object spawn issues 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