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
1
Question by Setzer22 · Jun 10, 2012 at 01:01 PM · structdeclare

How to declare a struct in C#

Hello everyone.

I'm trying to create a simple struct to hold data (not methods, just some properties), and I want it to be accessible from all my other scripts, like if it was just another class from the unity engine (Vector3 for example).

So I mostly know the differences between struct and class, and I want it to be a struct, not a class.

So how should I do it? As far as I know, unity doesn't let me attach a script with just a struct declaration, scripts are classes which inherit from monobehaviour, and structs cannot be inherited from classes (correct me if i'm wrong) so how do I declare the struct? Should I declare it inside of a public class, and then attach that script onto a gameObject? Or is there a better way to do this?

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
6
Best Answer

Answer by syclamoth · Jun 10, 2012 at 01:16 PM

You can't just stick a struct onto a gameobject, since it requires functionality that inherits from 'Component' in order for that to work. however, you can just declare structs in your files, the normal way:

 public struct MyStruct {
     // variables
 }

Keep in mind that structs will not be serialized by Unity, and as such cannot be used to save data between sessions. If all you are using them for is data transfer (messengers), this is fine- but if you want them to be serialized and visible from the inspector, you will need to declare them as classes, and use the 'System.Serializable' attribute.

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 Setzer22 · Jun 10, 2012 at 01:42 PM 1
Share

Just tested it and it worked, but I don't fully understand why.

So if I define a struct inside a script (not into the class which corresponds to that script), where am I actually defining it? Inside monoBehaviour? Is because of that that all the other scripts which inherit from monoBehaviour recognise my new variable type and are able to use it?

Thank you ^^

avatar image syclamoth · Jun 10, 2012 at 02:56 PM 1
Share

If you define a struct outside of any classes (in a script which also contains a monobehaviour), you are placing it in the 'default' namespace (which can be accessed from everywhere, without the need for 'using' directives). You can have any number of classes (or structs) in a single file- for the purposes of adding components Unity will only care about the one class which shares a name with the file, and only if it inherits from '$$anonymous$$onoBehaviour'. I usually have several files filled with 'helper' classes- these don't really attach to any specific GameObjects, and as such don't need to derive from $$anonymous$$onoBehaviour- but I use them in classes which do, which is how I can link them to my projects.

avatar image Setzer22 · Jun 10, 2012 at 03:04 PM 1
Share

That was helpful, I was also wondering a while ago how I could create a helper class to hold some useful methods which could be useful in all my scripts, now I know how to do it.

Thank you very much

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Structs in C# Question 3 Answers

C# if If Statement is True Declare Variable 1 Answer

Structs passed by ref = better performance? 2 Answers

Where to use Structs and classes? 5 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