Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 FuntikStudio · Jan 15, 2021 at 02:29 PM · objectinstance

Save Unity.Object as an asset

Hi guys,

I am trying to use AssetDatabase.CreateAsset to save some custom made data (grid in this example). I know that in order to use CreateAsset, I need to pass an Unity.Object instance. So I create a specific class that inherits from Unity.Object and have just one field that holds my data.

 public class GridSaveObject : UnityEngine.Object
 {
     public GridMapData gridMapData;
 
     public GridSaveObject(GridMapData gridMapData)
     {
         this.gridMapData = gridMapData;
     }
 }

Now, inside the custom inspector (in Editor mode only, but I don't think that is the problem here), I make a call:

 var obj = new GridSaveObject(new GridMapData());
 Debug.Log(obj== null); // returns true

I don't even see the reason to mention anything that goes after that, cause what's the point. The question, how does one create an instance of such objects? I know it is possible, cause the unity way of creating Materials is the same var mat = new Material (some_link_to_shader);

Comment
Add comment · Show 1
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 FuntikStudio · Jan 15, 2021 at 02:39 PM 0
Share

One way that I could fix that was to go ScriptableObject, and it fixed the problem, but for me it is a question of general understanding of the principle I guess

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Namey5 · Jan 16, 2021 at 03:08 AM

Unity Objects aren't generally supposed to be inherited from directly and also have some weird quirks that you would be experiencing here. 'Object == null' doesn't do what you are probably expecting - in order to guarantee safety with Unity's object instancing systems, Unity overloads the '==' operator for objects such that they do additional checks against the scene rather than just checking if the object pointer is valid. In the case of a GameObject this would work because the GameObject() constructor automatically adds it to the scene and initialises it, however because Object is just a generic base class that isn't the case and therefore you would need to handle it manually. This exact use case is actually shown as an example in the docs at the bottom of the 'Object.operator ==' page;

https://docs.unity3d.com/ScriptReference/Object-operator_eq.html

ScriptableObject inherits from UnityEngine.Object and is designed pretty specifically for this case; serializing an object to a Unity asset. ScriptableObjects should be created via the CreateInstance method, so I've added a static function to work in the same way your constructor would above.

 [System.Serializable]
 public class GridSaveObject : ScriptableObject
 {
     public GridMapData gridMapData;
  
     public static GridSaveObject NewInstance(GridMapData gridMapData)
     {
         GridSaveObject instance = ScriptableObject.CreateInstance<GridSaveObject>();
         instance.gridMapData = gridMapData;
         return instance;
     }
 }
 
 ...
 
 GridSaveObject obj = GridSaveObject.NewInstance (new GridMapData ());
 AssetDatabase.CreateAsset (obj, "GridObject.asset");

I would also make sure to mark your GridMapData class as [Serializable] if you want it to work properly with the Unity Editor/serializer.

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 FuntikStudio · Jan 18, 2021 at 09:21 AM 0
Share

Thanks a lot for the explanation!

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

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

Object not set to an instance of an object, but the variable is already declared 1 Answer

Referencing Objects in Unity 1 Answer

InstanceIDtoObject namespace? 1 Answer

How would I set an instance of an object? 1 Answer

Instantiated Object Individual properties through Script 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