✅ Creating a memory game! But a function always makes the last vector in array (0,0,0)?
So I've been building langauge learning activtities for English and Japanese speakers on VR Chat. This project is using Unity and Udon Sharp (VR chat friendly C#). I am currently building a memory game where there is a grid of 16 cards with 8 pairs of words. Take a look at the pictures.
In U#, you're not able to use lists or dictionaries... which isnt great. Everything ends up being arrays.
This function randomizes the order of elements in a array of vectors that allows me to then place the cards in random order. However, for some reason this functions always causes the last vector in the array to be (0f, 0f, 0f).... What I ended up doing is bumping up the number of elements to the array to 17 and just never using the last one... but this is janky...
I know there are much better algorithms to shuffle an array, and I`m open to checking those out too. But part of me is really curious why this is happening. It uses Unity's Random.Range to choose a random spot on the array and try to move the first original array element there. If it fails, it tries the next spot until it finds and open place. I put 20 for the amount of attempts it can make which is unnecessary, but if at any point one attempt loops all the way around it leaves the while loop.
I just tried to include the information that was necessary. Just started programming daily with C# a month or two ago, and it's my first time posting here, and I currently hate this function. So any advice, comments, suggestions, or insights are welcome.
よろしくお願いします
20 Replies
There is already a second 0 0 0 in the array
It does this when, I lower it to 16 too.
Vector3[17]
Why? You have 16 cards
And then you have that -1 in your for loop
Thank you for replying.
Yes! That's the question I'm asking.
When I change it to 16, I get this:
Because you're using n-1 in your for loop
i=0; i<n
That's what 99% of loops should be
This is good to know.
'For' loops, that is
I still get a missing piece though. hmmmm
Most loops in c# going to be foreach
You'll have to debug to investigate further
Huh, I've been using way more for loops.
I like using them to do things with multiple arrays. But this might be causing the trouble...
Yeah, I was going at it for a while. Though I might as well post and see if I wasn't missing something blatantly obvious.
Thanks for commenting.
Another round of debugging it is.
Ahhhhh! This was great info!
It was another function.
i < xxx.Length - 1
No bueno.
🙏 Thank you! 🙏
👍🏻
Woowoo!