Arrays in Structs in C#

My structs are really messy. I’m having to use things like:

public struct bobStructure
{
int bob1;
int bob2;
int bob3;
}

when what I really want is:

public struct bobStructure
{
int bob[3];
}

Arrays in Structs don’t seem to be supported. Anyone got a good work around for this??

int[ ] bob = new int[3];

Try that. :slight_smile:

unfortunately not :frowning: I get this error:

Assets/Scripts/map.cs(51,22): error CS0573: `map.mapStructure.bob’: Structs cannot have instance field initializers

how about

fixed int bob[3];

:slight_smile:

Nope :frowning: need to be defined as unsafe and Unity doesn’t support unsafe…

Curses…

Nic.

Erm… just realised. Should point out this is Unity iPhone - so it’s .net 1.0?? I think.

You can have an array in a struct, but you can only declare the size of the array at runtime. You need to declare an int[ ] field and then set up the array in the constructor:-

struct ExampleStruct {
    int[] ints;

    public ExampleStruct() {
        ints = new int[3];
    }
}

^^ That’s what I was getting at! I was close.

Damn - I feel horrible to keep saying nope, but here’s the error I get with the above example:

error CS0568: Structs cannot contain explicit parameterless constructors

This is the line causing the error:

public ExampleStruct()

Try this.

struct ExampleStruct {
    int[] ints;

    public ExampleStruct(int[] _ints) {
        ints = _ints;
    }
}

And to create an instance of the struct:

int[] ints = new int[13];
new ExampleStruct(ints);

Hi, are you using C#? I’m pretty sure C# does not allow parameterless struct constructors, so alternatively you could perhaps add a constructor with a parameter specifying the length of the array(s), or add an Initialize() function to the struct that does all the array initializing. I’m not sure if these suggestions are very ideal/efficient ways of handling it though, but they seems to work fine.

Some good news! The struct doesn’t throw up an error. However, the instance does :frowning:

ExampleStruct _ExampleStruct(ints);

throws up the error:

Identifier expected

I’m close to giving up. It seems like a crazy .net 1.0 limitation. :roll:

But I have to say thank you to all everyone for jumping in to help. It’s a great example of how cool the Unity community is.

Here ya go. This works fine.

public class NewBehaviourScript : MonoBehaviour {

	public struct bobStructure 
	{ 
		public bobStructure(int[] temp )
		{
			bob = temp;
		}
		public int[] bob;	
	}

	bobStructure t;
	

	void Start () {
		t = new bobStructure(new int[3]);
	}
	
	void Update () {
		Debug.Log(t.bob.Length);
	}
}
1 Like

Awesome. Thank you so much guys. This is does indeed compile and work!

Now to tidy up my code!

Just to clarify:

ExampleStruct _ExampleStruct(ints);

This throws error because the syntax for instance an object it’s:

type instance_name = new type(parameters);

This is how it should look like:

ExampleStruct _ExampleStruct = new ExampleStruct(ints);

Only for completeness: struct, array, constructor, prevent, prevent garbage collection

using UnityEngine;
using System.Collections;

public class TestAnything : MonoBehaviour {

    public struct bobStructure
    {
        public bobStructure(int[] b1, int[] b2, float[] s1, string[] d1)
        {
            bob1 = b1;
            bob2 = b2;
            joe = 0;
            sue = s1;
            doe = d1;
        }
        public int[] bob1;
        public int[] bob2;
        public float joe;
        public float[] sue;
        public string[] doe;
    }

    static int maxdoe = 99;
    public bobStructure t = new bobStructure(new int[3], new int[3], new float[4], new string[maxdoe]);

    void Start () {

        t.joe = 0.7777777f;
        t.sue[3] = 39;
        t.doe[98] = "John";

        Debug.Log("t.bob1[] " + t.bob1.Length);
        Debug.Log("t.bob2[] " + t.bob1.Length);
        Debug.Log("joe " + t.joe);
        Debug.Log("t.sue[3] " + t.sue[3]);
        Debug.Log("t.doe[98] " + t.doe[98]);
    }
   
    void Update () {
    }
}
1 Like