• 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

· Add your 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 w$$anonymous$$ch 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 t$$anonymous$$s 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Getting any type in Unity Inspector 1 Answer

Access a class on another script? 1 Answer

Calling an object from a different JS Script and Instantiate it 1 Answer

Serialization - Variables won't change on original construction 1 Answer

Can I make a list of hashtables or classes with pragma strict 2 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