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
4
Question by SisterKy · Dec 25, 2009 at 11:10 PM · scriptingbasicsdocumentationoop

What's the difference between a struct and a class?

I noticed that some entries in the API under "Runtime Classes" don't have "Class" as description, but "Struct".
So what's the difference?

Thanks & Greetz, Ky.

Comment
Add comment
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

1 Reply

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

Answer by Mortim · Dec 26, 2009 at 12:12 AM

There are many differences between structs and classes. Basically, structs are meant for lightweights objects or data grouping, and do not allow inheritance.

Here is a detailed comparison between structs and classes in C#

http://www.jaggersoft.com/pubs/StructsVsClasses.htm

You can also check the official msdn documentation on structs data types, which I cannot post here due to the one link limitation for new users. Google "msdn struct" and you should find it.

Comment
Add comment · Show 3 · 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 SisterKy · Dec 26, 2009 at 01:07 AM 0
Share

Oh, wow Oo ... I didn't think there were THIS many differences as they are both listed among "Runtime Classes" after all...
Thank you for your answer, I'm sure this is helpful to a lot of people. =)
For me however, being a bloody newbe to program$$anonymous$$g, I don't understand as much as half of what your link tries to tell me ^^' Would probably take me several full days to work through it -.- I'd really apreciate some simplified explaination... pretty please? :) (as well as a few words as to wether JavaScript handles structs exactly the same in this case...)

avatar image Mortim · Dec 26, 2009 at 02:11 AM 2
Share

You can view a struct as a simplified class. From a user perspective, it should not make many differences wether your object comes from a struct or a class. From a design perspective, struct are commonly used for simple and lightweight objects.

As far as I know, there is no struct in javascript. However you can emulate them by using simple objects. See here for an example : http://bytes.com/topic/javascript/answers/89608-datastructures-javascript

avatar image jashan · Dec 26, 2009 at 09:45 AM 1
Share

A $$anonymous$$or correction (this is also in the link you posted): One major difference between structs and classes is that the instances of structs live in the stack, while classes live in the heap. That's why very frequently used structs like Vector3 are implemented as structs ins$$anonymous$$d of classes. In C#, there's this one tricky thing that you can't change member variables of structs when those structs are properties of other objects (try transform.position.x = 3F; in C# to see what I mean).

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

No one has followed this question yet.

Related Questions

Not every Class in the API is derived from the "Object"-Class, so what are they? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Does Unity support multiple Inheritance? 3 Answers

Does Unity support the declaration of several classes in one script? 2 Answers

What is a Script when it's attatched to a GameObject? An Object? A Class? A Reference? 3 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