Declaration of structures

Started by
9 comments, last by 4d69636861656c 8 years ago

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.

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.


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

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

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?

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.


Error messages.

To be blunt, you're wasting everyone's time - including your own - when you post questions like this with providing the exact and complete error messages.

Pretend the tables are turned, we're your parents asking for IT help, and we're saying "my safari internet explorer won't open on linux vista, what's wrong" kinds of questions, then provide us with the same information you would request from us. :)

Sean Middleditch – Game Systems Engineer – Join my team!


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.

Hello to all my stalkers.

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.

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

This topic is closed to new replies.

Advertisement