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
1
Question by spaceduck_corp · Jan 09, 2015 at 07:09 PM · editor-scriptingnullreferenceexceptionscriptableobject

Editor Window/Scriptable object - null problem

I have the database that is basically a scriptable object (CellsDatabase):

     [System.Serializable]
     public class CellClassModification
     {
         public Type name;
         public int price;
     }
     
     [System.Serializable]
     public class CellClassData
     {
         public Type name;
         public GameObject prefab;
         public List<CellClassModification> modifications;
         
         public CellClassData()
         {
             name = null;
             prefab = null;
             modifications = new List<CellClassModification>();
         }
     
         public CellClassData(Type type)
         {
             name = type;
             prefab = null;
             modifications = new List<CellClassModification>();
         }
     }
     
     [System.Serializable]
     public class CellsDatabase : ScriptableObject
     {
         public List<CellClassData> cells;
     
         public CellsDatabase()
         {
             cells = new List<CellClassData>();
             SyncClasses();
         }
     
             . . .
     }

And the editor window for this database

alt text

Each class has a list of upgrades (CellClassModification) that instance of this class can transform into, paying the price.

Every time you open editor window it loads data from the same scriptable object. And it works good. But after Unity recompiles scripts or Unity restarts I'm getting null reference exceptions here:

 activeCellClass.modifications[a].**name**.Equals(...)

where:

  • modification is an instance of CellClassModification,

  • modifications[a].name == null (But it shouldn't)

Editor Window script

I don't understand how does modification.name get null value. Maybe some serialization issues?

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 VesuvianPrime · Jan 09, 2015 at 07:08 PM 2
Share

Type isn't serializable. You can look at converting it to/from string.

avatar image spaceduck_corp · Jan 09, 2015 at 07:50 PM 0
Share

mercy, sir

2 Replies

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

Answer by vexe · Jan 09, 2015 at 08:02 PM

As @VesuvianPrime said, System.Type is not serializable, you need to use a wrapper that has the type qualified asm name (not just name) serialized inside, and reconstruct the type when the internal value becomes null (when an assembly reload happens). This is how I used to do it in the pre-VFW days :p

 [Serializable]
 public class SerializedType
 {
     [SerializeField]
     private string asmQualifiedName;
     private Type value;
     
     public Type Value
     {
         get { return value ?? value = string.IsNullOrEmpty(asmQualifiedName) ? null : Type.GetType(asmQualifiedName));
         set
         {
             this.value = value;
             asmQualifiedName = value == null ? string.Empty : value.AssemblyQualifiedName;
         }
     }
     
     public SerializedType(string asmQualifiedName)
     {
         this.asmQualifiedName = asmQualifiedName;
     }
     
     public SerializedType(Type type)
     {
         Value = type;
     }
     
     public SerializedType()
     {
     }
 }

In VFW, System.Type serialization (and exposure) is directly supported, no need for wrappers 'n stuff.

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 spaceduck_corp · Jan 10, 2015 at 09:02 AM 0
Share

thanks for the help, I may use this when it comes to optimization

avatar image
0

Answer by spaceduck_corp · Jan 10, 2015 at 09:00 AM

I did it the way @VesuvianPrime said and it worked well.

So I replaced Type field in data classes by string field and when I nead an actual type I use Type.GetType("className").

It may not be the most effiecent way of doing things, but it works.

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 VesuvianPrime · Jan 10, 2015 at 02:35 PM 0
Share

Vexe's solution isn't more "optimized", it's more reliable. By simply converting the Type to/from string you're neglecting cases where two Types might have the same name.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

ScriptableObject reference resetted after a method is finished. 2 Answers

ApplyModifiedProperties returns false 0 Answers

Why is play mode reverting my scriptableobject to a previous serialized state? 2 Answers

OnInteractivePreviewGUI multiple selection. Works but spams errors. 0 Answers

How to work with custom class (ScriptableObject) SerializedProperty? 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