# 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.

## 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 on other sites

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 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 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 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 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 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 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 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

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002274
×