Jump to content
  • Advertisement
Sign in to follow this  
4d69636861656c

Unity Declaration of structures

This topic is 828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How can one declare a structure (more specifically an array of structures) inside another structure in C#? Tried something I found on the Microsoft website and it doesn't appear to work for Unity. If anyone could provide me an example or point me in the right direction, that would be extremely helpful ...

Thanks.

Share this post


Link to post
Share on other sites
Advertisement

Because C# structs are value types, rather than reference types, fixed arrays like that are not permitted.

 

You can create a struct member that is a reference, such as:  public MyOtherStruct[] name;  You'll then need to allocate the array and manipulate it on your own.

 

You cannot create a fixed array except for primitive types, such as: public fixed int items[256];  However, that use is not recommended for several reasons. Value types should be kept small if possible.

Share this post


Link to post
Share on other sites
public struct SimpleStruct
{
    public int Foo;
    public int Bar;
}

public struct StructOfArrayOfStructs
{
    public SimpleStruct[] Elements;
}
When you say "Doesn't appear to work for Unity", EXACTLY what do you mean? Edited by Nypyren

Share this post


Link to post
Share on other sites
When you say "Doesn't appear to work for Unity", EXACTLY what do you mean?

 

No, I meant that for structures inside other structures, not for vectors of structures. I can't seem to be able to instantiate the variables. I'm not even sure if it's possible right now.

Share this post


Link to post
Share on other sites

I'm going to take a wild guess that you're trying to use an array of structs as a member of a MonoBehaviour or ScriptableObject.

If that's the case, you need to put the 'Serializable' attribute on your structs for them to appear in Unity's inspector and get serialized to asset files:
 

[Serializable] // using System;
public struct SimpleStruct
{
    public int Foo;
    public int Bar;
}

[Serializable]
public struct StructOfArrayOfStructs
{
    public SimpleStruct[] Elements;
}

Share this post


Link to post
Share on other sites

When you say "Doesn't appear to work for Unity", EXACTLY what do you mean?

 
No, I meant that for structures inside other structures, not for vectors of structures. I can't seem to be able to instantiate the variables. I'm not even sure if it's possible right now.


Ok, I posted my second message before I read this.

Structs have slightly different construction rules than classes. What are you trying? Can you show some code?

Share this post


Link to post
Share on other sites


when you post questions like this without providing the exact and complete error messages.

(Bold fix)

 

I would also add that you (4detc) should also include the actual code which produces those error messages.

Share this post


Link to post
Share on other sites

 

When you say "Doesn't appear to work for Unity", EXACTLY what do you mean?

 

No, I meant that for structures inside other structures, not for vectors of structures. I can't seem to be able to instantiate the variables. I'm not even sure if it's possible right now.

 

For reasons that only god knows, Unity uses a terribly outdated version of mono. Possibily for stability and support reasons. Though that won't make much sense give that prior to the upgrade to Unity 5, they upgraded to a SLIGHTLY newer version of mono. Which is really just maybe a slight step up the revision.

 

Now... are you talking about defining a structure inside a structure? If so I wasn't even aware that was a legal action.

Share this post


Link to post
Share on other sites

Now... are you talking about defining a structure inside a structure? If so I wasn't even aware that was a legal action.


It's allowed. I haven't seen it in real code yet, though...

I don't know of anything Mono or Unity specific regarding structs that differs from the usual .Net, either (besides internal implementation details, of course). Edited by Nypyren

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!