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 disuperguy · Jan 18, 2021 at 06:20 AM · objectfield

How can I have a class that inherits the UnityEngine.Object baseclass

I know it's possible so please don't say it's "impossible" because meshes, sprites, materials, etc can do it. Don't ask why I need to, just answer the question, please, I'm pulling my hair out about this and I can't find any documentation on it.

Comment
Add comment · Show 3
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 Llama_w_2Ls · Jan 18, 2021 at 10:09 AM 0
Share
 public class NewClass : Object
 {
 
 }

??

avatar image Hellium · Jan 18, 2021 at 10:11 AM 0
Share

You will need to provide more information than that....

 public class $$anonymous$$yObject : UnityEngine.Object
 {
 
 }

does not throw any error.


Do you actually want to have the possibility to create an asset?

Why not using a ScriptableObject where you can save every information you want?

avatar image disuperguy Hellium · Jan 18, 2021 at 11:55 AM 0
Share

I wanted to use the ObjectField in the editor because I wanted to make a $$anonymous$$esh type that could hold more than 3 dimensions. Also sorry if I came off as rude, thank you.

1 Reply

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

Answer by Bunny83 · Jan 18, 2021 at 12:43 PM

No, you can not and should not derive any class from UnityEngine.Object. It's just a base class wrapper for Unity's internal native classes. You can derive your own class indirectly from UnityEngine.Object by using either ScriptableObject or MonoBehaviour as base class. Both are derived from UnityEngine.Object and represent a component that is defined on the managed / mono side.


UnityEngine.Object instances require a native counterpart, otherwise they can not be handled by the engine (Keep in mind that Unity is written in C++, not C#). If you derive your own class from UnityEngine.Object or if you create such an instance with "new", it will not have a native counterpart. This means the object will pretend it is null when you compare it to null. Also Unity can't really handle the class on the native side where the serialization happens.


So for a custom Mesh class you should use ScriptableObject as base class. It's the base for many other internal editor classes as well. For example EditorWindow and Editor are also ScriptableObjects.


Just keep in mind to create instances with CreateInstance.


Note that the golden rule to never create such objects with "new" has a few exceptions. Namely the Mesh class as well as the GameObject class. Actually the same rules apply to those behind the scenes. However the managed classes do create the native part from inside the managed constructor. Richard Fine (Unity empolyee) even mentioned in his ScriptableObject talk that he doesn't know why they didn't do this for ScriptableObject as well.


Note that the only purpose of the UnityEngine.Object is to provide the native object binding, nothing else. See for yourself. Also here's the GameObject constructor and here's the Mesh class constructor.


Custom classes have to be derived from ScriptableObject or MonoBehaviour, at least when you want them to be serialized as assets.

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

113 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

Related Questions

Array of objectFiled in editorWindow 1 Answer

Prefabs not showing up in the object browser 1 Answer

Custum editor: How do I setup a 3x3 grid of ObjectFields? 2 Answers

How can I get a list of specific game objects in the object picker opened from an ObjectField()? 0 Answers

Strange error in EditorGUILayout.ObjectField() 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