Can I make an array of structs ? (Newbie)

Ok, so this question might sound very stupid, since I have no idea what I’m talking about.

Let’s say I’m making a 2D minecraft game. I have a 2D array (Cell[x,y]) representing each block the world.

Now lets say I have a script with this code :

using UnityEngine;
using System.Collections;

public struct Earth
{
    public bool Solid;
    
    public static void SetUV(int FirstVertice)
    {
    MeshControl.newUVs[FirstVertice]   = new Vector2(0000/1024F,1016/1024F); 	
    MeshControl.newUVs[FirstVertice+1] = new Vector2(0000/1024F,1024/1024F); 
    MeshControl.newUVs[FirstVertice+2] = new Vector2(0007/1024F,1024/1024F); 	
    MeshControl.newUVs[FirstVertice+3] = new Vector2(0007/1024F,1016/1024F);
    }
}

public struct Empty
{
    public bool Solid;
    
    public static void SetUV(int FirstVertice)
    {
    MeshControl.newUVs[FirstVertice]   = new Vector2(1,1); 	
    MeshControl.newUVs[FirstVertice+1] = new Vector2(1,1); 
    MeshControl.newUVs[FirstVertice+2] = new Vector2(1,1); 	
    MeshControl.newUVs[FirstVertice+3] = new Vector2(1,1);
    }
}

How could make my array point to structs ? For exemple Cell[100,234] would be Empty and Cell[25,36] would be Earth ? And then could i do something like Cell[x,y].SetUV(Bob) to call the struct’s at cell[x,y] function ?

On a side note, it won’t let me set the variable solid. How can I make it true for the first one and false for the second ?

Thank you
Tabc3dd

???

Array:

 MeshControl.newUVs[FirstVertice]

Struct:

  new Vector2(0000/1024F,1016/1024F)

Array of struct’s:

MeshControl.newUVs[FirstVertice]   = new Vector2(0000/1024F,1016/1024F);

Sorry, I knew I was kind of saying random nonsense, but I don’t know how to make it clear.

Basically, I want to get a way to have an array that points towards something that contains functions, so that I can call :

Cell[x,y].MyFunction(MyVar)

and get a result.

Thank you
Tabc3dd

(Now trying to use classes and objects, not getting any results either :confused: …)

Should work perfectly fine with classes or structs.

Could you make a simple exemple please, nothing I do seems to work :confused:

For exemple, I tried doing it step by step but even my first step fails :

static public struct[,] Cell;

I get “Identifier expected”

Sorry
Tabc3dd

Unfortunately, that was not was I was looking for, however thank you very much for all your help and sorry.

Let’s say I have a few structs named “Struct1”, “Struct2”, “Struct3” and “Struct4” . Each of these struct has a function named MyFunction().

How could i have an array that is for exemple :

MyArray[0] = Struct1
MyArray[1] = Struct1
MyArray[2] = Struct3
MyArray[3] = Struct4
MyArray[4] = Struct2
MyArray[5] = Struct4

And then make it so that I could call a function like so :

MyArray[4].MyFunction()

If you’ve had enough, I understand and will just keep testing and messing around till I find ^^
Thank you
Tabc3dd

Simply said no. I would suggest using a dictionary storing the key as you 2D array and an enumeration to know what struct to point to.

private Dictionary<int[][],int> _twoDee = new Dictionary<int[][],int>();

then set up an enum for the second int that lets you know which struct its for:

public enum StructType : int
{
Empty = 0,
Earth = 1
}

Then you’ll need some if statements to determine which struct’s to call a function in.

I hope this helps some.

Thank you, looks like I’ll need to look at another method then

Are these structs different type?
If they are the same type then it’s simply

class Struct
{
     var structName : String;
     function Struct(str : String)
     {
          structName = str;
     }
     function PrintName()
     {
          Debug.Log(structName);
     }
}

function Awake()
{
    var structs : Struct[] = new Struct[5];
	
    for(i = 0; i < structs.length; i++)
    {
        structs[i] = new Struct("struct " + i);
    }
    for(i = 0; i < structs.length; i++)
    {
        structs[i].PrintName();
    }
}

Thank you very much, I’ll see what I can do with this ^^