❔ Error. Array initializer can only be used in a variable or field initializer.
Initialize it from the constructor perhaps?
91 Replies
Still not working
Ah, you might simply need
new[]s for each array
Something thisaboutsYou have mismatched dimensions
Orannis#3333
REPL Result: Success
Compile: 357.500ms | Execution: 55.271ms | React with ❌ to remove this embed.
The mismatched dimensions is correct for 1 thing.
Well, in your original picture, they're mismatched
You are assigning a
int[,,,] to a int[,,]Yes, I've corrected that. It does not fix my current issue though.
Well, then can you please post an updated version that is giving you an error, and what that error is 🙂
I'm assuming the error is the name of the post, but your repl runs
Array initializers can only be used in a variable or field initializer. Try using a new expression instead.

i've got unity up I will give it a go on my computer
You have mismatched dimensions still
What do you mean? I'ts in a 1, 6, 3 ,3 dimensional format
you have five layers of {
Look at my example, and compare the number of
{ } pairs
There are no other issues with unity, that is it, it just gives a lil bit of a weird error for it
You declared a
[1, 1, 6, 3, 3] array
Not [1, 6, 3, 3] oneThen tell me why I get an error when I remove as requested?
copy and paste the new code?
I'll do the job, again. and then post
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?
Yes
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.

you have one too many closing brackets
I think, or one too little opening
Whups. Slight screw up.
Yes, missing one opening
Fixed. The situation is still the same.

Line 17 and line 18.
Instead of adding 1 opening, you deleted 1 closing
those aren't 3,3, they are 9 values
Again, compare what you have to this
This compiles
It shouldn't. It's missing a brace.
No it isn't
That's why I'm getting the same issue
You may have overlooked the brace on the first line
I think this is what you want?
same thing as 333fred was saying but also you have one of the lengths wrong
I copied and pasted. it is wrong.

that isn't what I put
.
int[1,6,9,3], copy it exactly
Also, copy and pasted

You still missed the brace on the first line
I'm not wanting 9. I'm wanting 3...
your not though, what you put is this
OKay. I see that perhaps I should explain this...
if you are wanting three you want what 333fred put originally
with the extra brace
Literally copy/paste the entire snippet I put originally
Not just the initializer, because you keep missing the first opening brace from it
unless you want this? This is more of the structure that you could infer from the first one
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.
is this a mesh for unity?
No. Please keep that thing about being unity out of this.
Orannis#3333
Literally copy/paste the entire snippet I put originally
Quoted by
<@!269674004065943552> from #Error. Array initializer can only be used in a variable or field initializer. (click here)
React with ❌ to remove this embed.
so you want the 1,6,9,3 one I sent?
That's how I would interpret it
3 values/spot, 9 spots/face, 6 faces/thing, 1 thing
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?
I don't know it depends what you are doing with it
the way you described it here you wanted 1,6,9,3
This is kind of how I was aiming it

of course this is just in 2D
Its uh... a visual guide if I must say.

I'm unclear why you would need 3 values/spot, given that
oooh cool, I am thinking from that that the 1 is unnesercary but you might have multiple?
Seems like you'd have 3 rows and 3 columns, each with a value
It is so that this particular defined spot holds a very specific value.
So
new int [6 /* sides */, 3 /* rows */, 3 /* columns */]
But is that value really 3 ints?That was the idea...
Or is that value a specific int
ok good
It is 3 ints (could be smaller even). Those 3 ints represent face, row column- even though it is just a theoretical piece
But you already have row and column represented
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
IE:
myArr[1, 2, 3] = 4 says that there's a 4 in face 1, row 2, column 3Right, 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.
I think this was right then
dude 0 indexing smh
but yeah what he said is applicable for all the faces
shhh
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.
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 syntaxby what you have now are you talking about something like this?
Next: make a
Piece record struct type, and use that as your array typewith the 1 @Orannis he meant to have multiple rubix cubes at some point
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
probably makes more sense to encapsulate it and have an array of that class though
yeah
Not an extra dimension in this array
yeah true
This is what I'm going for. Thank you for the help.

Much better
I need to look at record structs I haven't worked with them before
I would recommend that
Piece struct to replace the inner 3 though
IE, new Piece[6, 3, 3] { ... { new Piece(0, 0, 0), ... } ... }unity won't serialize that
you need wrappers
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.