• 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
Question by Nemox · Nov 17, 2014 at 08:35 PM · objectclassvaluereturnserializable

Return a value for a class?

So I have a serializable class, and I want to be able to get a value simply by referring to the object. At the moment I have to refer to specific values like "statistic.total" when I'd rather just say "statistic".

How might I go about returning the object's value?

Comment
Bunny83

People who like this

1 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 rob5300 · Nov 17, 2014 at 10:31 PM 0
Share

I believe you cannot do this, I don't think a class can somehow be like a instance of a class.

avatar image Nemox · Nov 17, 2014 at 10:36 PM 0
Share

Hm... perhaps a struct would describe what I want. A Vector3 is a struct with three variables and a few default values. If you refer to the vector itself, it will provide the variables together.

I'm wondering if there would be a similar thing for classes. If not, learning how to do this with structs would be helpful as well.

avatar image richyrich · Nov 17, 2014 at 11:55 PM 0
Share

Do you want to create a custom defined type (something that holds a load of values together)?

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Nov 18, 2014 at 12:00 AM

The Vector3 struct is always a struct and a class is always a class. You can implement implicit casting operators, however that should only be done when it's an obvious conversion. The Vector2 / 3 / 4 struct only have a conversion between each other so a Vector2 can be used as Vector3.

Implicit casting only applies when it's obvious for the compiler to which type it should be converted.

Example:

 public class Statistic
 {
     public float total;
     public static implicit operator float(Statistic aObj)
     {
         return aObj.total;
     }
 }

An instance of that class could be converted implicitly into a float value:

 Statistic statistic = new Statistic();
 statistic.total = 5;
 // ...
 float f = statistic;
 // f would be "5"

However, in this case it's not really obvious and an conversion like that isn't convenient and shouldn't be implemented like that. Implicit casting isn't ment for laziness but when one type can be used like another. Another example is "Color" and "Color32".

Comment
Kiwasi
richyrich

People who like this

2 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

30 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

Related Questions

Access a class on another script? 1 Answer

Getting any type in Unity Inspector 1 Answer

Not serializable anymore when also a prefab 0 Answers

BOO: Unexpected Errors 1 Answer

Get GameObject transform inside a custom class 0 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