- Home /
Multidimensional array of objects in C#
Hi, I'm trying to store the position and angle for each object, for each frame. (It is for a project not related gaming, so don't worry about real-time performance.) I'd like to create a matrix of objects (each object containing the game object ID and position), but since I am new both to Unity and C#, I cannot get it to work. Based with the examples I wrote it like this:
[System.Serializable]
class ObjPosInfo
{
public string Obj_ID;
public float Pos_x;
public float Pos_y;
public float Angle;
public ObjPosInfo(string Obj_ID, float Pos_x, float Pos_y, float Angle)
{
this.Obj_ID = Obj_ID;
this.Pos_x = Pos_x;
this.Pos_y = Pos_y;
this.Angle = Angle;
}
}
void Start () {
ObjPosInfo[,] frameArray = new ObjPosInfo [numberOfFrames, GameObject.FindGameObjectsWithTag("box")];
}
When I try to execute, this error appears: Assets/test_script.cs(41,87): error CS0029: Cannot implicitly convert type UnityEngine.GameObject[]' to
int'. What am I doing wrong?
And how do I access later this frameArray from other parts of the code?
Well, that's a little more complicated. I'll edit my post for you.
How much program$$anonymous$$g experience do you have? What languages are you familiar with?
Answer by syclamoth · Jan 12, 2012 at 11:23 AM
It's not a big problem- all you forgot to do was put the .Length at the end of the array!
When you declare a multidimensional array, you need two integers, for the two dimensions. However,
GameObject.GindGameObjectsWithTag(string);
returns an array of gameObjects, not an int! Of course, obviously you just want the length of the array (assuming you're going to populate your array later), so all you need to do is change that line to
ObjPosInfo[,] frameArray = new ObjPosInfo [numberOfFrames, GameObject.FindGameObjectsWithTag("box").Length];
Of course, if you want to be able to see this new array from other parts of your code, you need to define it in a shared scope. When you write a method, like so-
public void foo(Bar bar)
{
// stuff happens here
}
any variables declared inside the curly brackets cannot be seen outside of that function. They are created and destroyed entirely within the scope of those brackets!
If you want to use a variable over several functions, you need to make it a class member.
Of course, if you declare the variable at the top of your class, before any functions, you can't create it on the same line that it is declared, because GameObject.FindGameObjectsWithTag doesn't really mean anything at compile-time. Instead, you should create it like this-
public class ThisIsYourClass : MonoBehaviour { ObjPosInfo[,] frameArray;
void Awake()
{
frameArray = new ObjPosInfo [numberOfFrames, GameObject.FindGameObjectsWithTag("box").Length];
}
}
Then, whenever you want to access this array from another function, just use
frameArray[number, otherNumber]
to get a member, or just 'frameArray' if there's a function you want to do on the array itself.
Just out of interest, is this a time-travel game?
I am mostly familiar with C, so objects are really a step forward. I think I will manage to do it with your lines. Thanks for the fast and helpful answer!
No, not a game at all, just an university project, but I like the idea of time-travel games :)
Well, the time-travel thing is because some friends of $$anonymous$$e are making a time-travel game, and part of it works by keeping track of an object's position at every single point in time, through an array of 'frames', much like yours.
Your answer
![](https://koobas.hobune.stream/wayback/20220613062919im_/https://answers.unity.com/themes/thub/images/avi.jpg)