C
C#3y ago
Angius

❔ Error. Array initializer can only be used in a variable or field initializer.

Initialize it from the constructor perhaps?
91 Replies
Gerald
Gerald3y ago
Still not working
Angius
AngiusOP3y ago
Ah, you might simply need new[]s for each array
arr = new int[]{
new[]{
new[]{1, 2, 3},
new[]{4, 5, 6}
},
new[]{
new[]{7, 8, 9},
new[]{10, 11, 12}
}
}
arr = new int[]{
new[]{
new[]{1, 2, 3},
new[]{4, 5, 6}
},
new[]{
new[]{7, 8, 9},
new[]{10, 11, 12}
}
}
Something thisabouts
333fred
333fred3y ago
You have mismatched dimensions
MODiX
MODiX3y ago
Orannis#3333
REPL Result: Success
int[,,,] field = new int[1, 6, 3, 3] {
{
{
{ 1, 1, 1 },
{ 1, 1, 1 },
{ 1, 1, 1 },
},
{
{ 1, 1, 1 },
{ 1, 1, 1 },
{ 1, 1, 1 },
},
{
{ 1, 1, 1 },
{ 1, 1, 1 },
{ 1, 1, 1 },
},
{
{ 1, 1, 1 },
{ 1, 1, 1 },
{ 1, 1, 1 },
},
{
{ 1, 1, 1 },
{ 1, 1, 1 },
{ 1, 1, 1 },
},
{
{ 1, 1, 1 },
{ 1, 1, 1 },
{ 1, 1, 1 },
},
},
};
int[,,,] field = new int[1, 6, 3, 3] {
{
{
{ 1, 1, 1 },
{ 1, 1, 1 },
{ 1, 1, 1 },
},
{
{ 1, 1, 1 },
{ 1, 1, 1 },
{ 1, 1, 1 },
},
{
{ 1, 1, 1 },
{ 1, 1, 1 },
{ 1, 1, 1 },
},
{
{ 1, 1, 1 },
{ 1, 1, 1 },
{ 1, 1, 1 },
},
{
{ 1, 1, 1 },
{ 1, 1, 1 },
{ 1, 1, 1 },
},
{
{ 1, 1, 1 },
{ 1, 1, 1 },
{ 1, 1, 1 },
},
},
};
Compile: 357.500ms | Execution: 55.271ms | React with ❌ to remove this embed.
Gerald
Gerald3y ago
The mismatched dimensions is correct for 1 thing.
333fred
333fred3y ago
Well, in your original picture, they're mismatched You are assigning a int[,,,] to a int[,,]
Gerald
Gerald3y ago
Yes, I've corrected that. It does not fix my current issue though.
333fred
333fred3y ago
Well, then can you please post an updated version that is giving you an error, and what that error is 🙂
Sherbert Lemon
I'm assuming the error is the name of the post, but your repl runs
Gerald
Gerald3y ago
Array initializers can only be used in a variable or field initializer. Try using a new expression instead.
Sherbert Lemon
i've got unity up I will give it a go on my computer
Angius
AngiusOP3y ago
You have mismatched dimensions still
Gerald
Gerald3y ago
What do you mean? I'ts in a 1, 6, 3 ,3 dimensional format
Sherbert Lemon
you have five layers of {
333fred
333fred3y ago
Look at my example, and compare the number of { } pairs
Angius
AngiusOP3y ago
Sherbert Lemon
There are no other issues with unity, that is it, it just gives a lil bit of a weird error for it
Angius
AngiusOP3y ago
You declared a [1, 1, 6, 3, 3] array Not [1, 6, 3, 3] one
Gerald
Gerald3y ago
Then tell me why I get an error when I remove as requested?
Sherbert Lemon
copy and paste the new code?
Gerald
Gerald3y ago
I'll do the job, again. and then post
Angius
AngiusOP3y ago
Well, remove it, and tell us what the error is It's easier to debug code that has fewer errors So let's remove the most obvious ones first, right?
Gerald
Gerald3y ago
Yes
Gerald
Gerald3y ago
Alright. So, I have tested it all out. Any "nest" of brackets I remove, it keeps telling me that an array initializer of length 1 is expected. It doesn't matter which nest as well.
Sherbert Lemon
you have one too many closing brackets I think, or one too little opening
Gerald
Gerald3y ago
Whups. Slight screw up.
333fred
333fred3y ago
Yes, missing one opening
Gerald
Gerald3y ago
Fixed. The situation is still the same.
Gerald
Gerald3y ago
Gerald
Gerald3y ago
Line 17 and line 18.
333fred
333fred3y ago
Instead of adding 1 opening, you deleted 1 closing
Sherbert Lemon
those aren't 3,3, they are 9 values
333fred
333fred3y ago
Again, compare what you have to this This compiles
Gerald
Gerald3y ago
It shouldn't. It's missing a brace.
333fred
333fred3y ago
No it isn't
Gerald
Gerald3y ago
That's why I'm getting the same issue
333fred
333fred3y ago
You may have overlooked the brace on the first line
Sherbert Lemon
int[,,,] field = new int[1, 6, 9, 3] {
{
{
{ 1, 1,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 1 },{ 1, 1, 1 },{ 1, 1, 1 },
},
{
{ 1, 1, 1 },{ 1, 1, 1 },{ 1, 1, 1 },
{ 1, 1, 1 },{ 1, 1, 1 },{ 1, 1, 1 },
{ 1, 1, 1 },{ 1, 1, 1 },{ 1, 1, 1 },
},
{
{ 1, 1, 1 },{ 1, 1, 1 },{ 1, 1, 1 },
{ 1, 1, 1 },{ 1, 1, 1 },{ 1, 1, 1 },
{ 1, 1, 1 },{ 1, 1, 1 },{ 1, 1, 1 },
},
{
{ 1, 1, 1 },{ 1, 1, 1 },{ 1, 1, 1 },
{ 1, 1, 1 },{ 1, 1, 1 },{ 1, 1, 1 },
{ 1, 1, 1 }, { 1, 1, 1 },{ 1, 1, 1 },
},
},
};
I think this is what you want? same thing as 333fred was saying but also you have one of the lengths wrong
Gerald
Gerald3y ago
I copied and pasted. it is wrong.
Sherbert Lemon
that isn't what I put
333fred
333fred3y ago
.
Sherbert Lemon
int[1,6,9,3], copy it exactly
Gerald
Gerald3y ago
Also, copy and pasted
333fred
333fred3y ago
You still missed the brace on the first line
Gerald
Gerald3y ago
I'm not wanting 9. I'm wanting 3...
Sherbert Lemon
your not though, what you put is this
Gerald
Gerald3y ago
OKay. I see that perhaps I should explain this...
Sherbert Lemon
if you are wanting three you want what 333fred put originally with the extra brace
333fred
333fred3y ago
Literally copy/paste the entire snippet I put originally Not just the initializer, because you keep missing the first opening brace from it
Sherbert Lemon
unless you want this? This is more of the structure that you could infer from the first one
private int[,,,] field = new int[6, 3, 3, 3]
{
{
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
},
{
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
},
{
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
},
{
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
},
{
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
},
{
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
},
};
private int[,,,] field = new int[6, 3, 3, 3]
{
{
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
},
{
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
},
{
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
},
{
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
},
{
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
},
{
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
},
};
Gerald
Gerald3y ago
Perhaps you guys need to know some more information... These numbers: "0,0,0" are a representation of where a value will be held. Each of these are individual spots. 9 of these spots are nested to a "face". There are 6 faces. 6 "faces" is represented as 1 thing.
Sherbert Lemon
is this a mesh for unity?
Gerald
Gerald3y ago
No. Please keep that thing about being unity out of this.
MODiX
MODiX3y ago
Orannis#3333
Literally copy/paste the entire snippet I put originally
React with ❌ to remove this embed.
Sherbert Lemon
so you want the 1,6,9,3 one I sent?
333fred
333fred3y ago
That's how I would interpret it 3 values/spot, 9 spots/face, 6 faces/thing, 1 thing
Gerald
Gerald3y ago
When I first saw you post that, it was my initial belief that you got exactly what I was looking for, but that "6" and "3" threw me for a loop. Now I just had to say "no" to fred. (hahaha. I got so frustrated). Why is it that the way I was interpreting it incorrectly?
Sherbert Lemon
I don't know it depends what you are doing with it the way you described it here you wanted 1,6,9,3
Gerald
Gerald3y ago
This is kind of how I was aiming it
Gerald
Gerald3y ago
of course this is just in 2D Its uh... a visual guide if I must say.
Gerald
Gerald3y ago
333fred
333fred3y ago
I'm unclear why you would need 3 values/spot, given that
Sherbert Lemon
oooh cool, I am thinking from that that the 1 is unnesercary but you might have multiple?
333fred
333fred3y ago
Seems like you'd have 3 rows and 3 columns, each with a value
Gerald
Gerald3y ago
It is so that this particular defined spot holds a very specific value.
333fred
333fred3y ago
So new int [6 /* sides */, 3 /* rows */, 3 /* columns */] But is that value really 3 ints?
Gerald
Gerald3y ago
That was the idea...
333fred
333fred3y ago
Or is that value a specific int
Sherbert Lemon
ok good
Gerald
Gerald3y ago
It is 3 ints (could be smaller even). Those 3 ints represent face, row column- even though it is just a theoretical piece
333fred
333fred3y ago
But you already have row and column represented
Sherbert Lemon
this given thing is 1,6,9,3, but now what you have described makes more sense to be 1,6,3,3, what is the other data in this one
333fred
333fred3y ago
IE: myArr[1, 2, 3] = 4 says that there's a 4 in face 1, row 2, column 3
Gerald
Gerald3y ago
Right, but I now need to do this to each and every other face. ... and I don't have that information (the value it equals to)working just yet.
Sherbert Lemon
I think this was right then dude 0 indexing smh but yeah what he said is applicable for all the faces
333fred
333fred3y ago
shhh
Gerald
Gerald3y ago
Trust me when I say this, what I currently have now is correct. If you kind of think about a Rubik's cube and how things rotate. these values and faces will start to jumble just like that.
333fred
333fred3y ago
I would start by removing the first 1 dimension from your array. It's not helpful, and it's just causing you to nest further, complicating the syntax
Sherbert Lemon
by what you have now are you talking about something like this?
333fred
333fred3y ago
Next: make a Piece record struct type, and use that as your array type
Sherbert Lemon
with the 1 @Orannis he meant to have multiple rubix cubes at some point
333fred
333fred3y ago
It will be generally helpful for you for tracking these pieces, not just in this array Sure, but you'll want an array of these arrays at that point
Sherbert Lemon
probably makes more sense to encapsulate it and have an array of that class though yeah
333fred
333fred3y ago
Not an extra dimension in this array
Sherbert Lemon
yeah true
Gerald
Gerald3y ago
This is what I'm going for. Thank you for the help.
333fred
333fred3y ago
Much better
Sherbert Lemon
I need to look at record structs I haven't worked with them before
333fred
333fred3y ago
I would recommend that Piece struct to replace the inner 3 though IE, new Piece[6, 3, 3] { ... { new Piece(0, 0, 0), ... } ... }
Anton
Anton3y ago
unity won't serialize that you need wrappers
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?