C
C#2y ago
Kuri_-

✅ 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
Kuri_-
Kuri_-OP2y ago
public void RandomizeTransforms()
{
//Make sure shuffled transforms is full of (-13f,0f,0f) vector3s
shuffledTransforms = new Vector3[17];
for (int i= 0; i < shuffledTransforms.Length-1; i ++)
{
shuffledTransforms[i] = noVectorYet;
Debug.Log($"i: {i}, {shuffledTransforms[i]}");
}
Debug.Log($"The length of shuffledTransforms is {shuffledTransforms.Length}");


int arraySpotsChecked = 0;
for (int i = 0; i < shuffledTransforms.Length; i++)
{
int entrancePoint = Random.Range(0, shuffledTransforms.Length);
if (shuffledTransforms[entrancePoint] == noVectorYet)
{
shuffledTransforms[entrancePoint] = cardTransforms[i];
Debug.Log($"Shuffle placement successful first try! Turn {i} {shuffledTransforms[i]}");
}
else
{
arraySpotsChecked = 0;
while ( arraySpotsChecked < 20)
{
entrancePoint++;
if (entrancePoint == shuffledTransforms.Length)
{
entrancePoint = 0;
}
if (shuffledTransforms[entrancePoint] == noVectorYet)
{
shuffledTransforms[entrancePoint] = cardTransforms[i];
Debug.Log($"Shuffle placement on the {arraySpotsChecked} Turn {i} {shuffledTransforms[i]}");
break;
}
else
{
arraySpotsChecked++;
}
}
}
}
Debug.Log($"The length of shuffledTransforms 2nd time is {shuffledTransforms.Length}");
for (int i = 0; i < shuffledTransforms.Length; i++)
{
Debug.Log($"{i} {shuffledTransforms[i]} {shuffledTransforms.Length}");
}
}
public void RandomizeTransforms()
{
//Make sure shuffled transforms is full of (-13f,0f,0f) vector3s
shuffledTransforms = new Vector3[17];
for (int i= 0; i < shuffledTransforms.Length-1; i ++)
{
shuffledTransforms[i] = noVectorYet;
Debug.Log($"i: {i}, {shuffledTransforms[i]}");
}
Debug.Log($"The length of shuffledTransforms is {shuffledTransforms.Length}");


int arraySpotsChecked = 0;
for (int i = 0; i < shuffledTransforms.Length; i++)
{
int entrancePoint = Random.Range(0, shuffledTransforms.Length);
if (shuffledTransforms[entrancePoint] == noVectorYet)
{
shuffledTransforms[entrancePoint] = cardTransforms[i];
Debug.Log($"Shuffle placement successful first try! Turn {i} {shuffledTransforms[i]}");
}
else
{
arraySpotsChecked = 0;
while ( arraySpotsChecked < 20)
{
entrancePoint++;
if (entrancePoint == shuffledTransforms.Length)
{
entrancePoint = 0;
}
if (shuffledTransforms[entrancePoint] == noVectorYet)
{
shuffledTransforms[entrancePoint] = cardTransforms[i];
Debug.Log($"Shuffle placement on the {arraySpotsChecked} Turn {i} {shuffledTransforms[i]}");
break;
}
else
{
arraySpotsChecked++;
}
}
}
}
Debug.Log($"The length of shuffledTransforms 2nd time is {shuffledTransforms.Length}");
for (int i = 0; i < shuffledTransforms.Length; i++)
{
Debug.Log($"{i} {shuffledTransforms[i]} {shuffledTransforms.Length}");
}
}
Kuri_-
Kuri_-OP2y ago
Kuri_-
Kuri_-OP2y ago
Kuri_-
Kuri_-OP2y ago
There is already a second 0 0 0 in the array It does this when, I lower it to 16 too.
Kuri_-
Kuri_-OP2y ago
phaseshift
phaseshift2y ago
Vector3[17] Why? You have 16 cards And then you have that -1 in your for loop
Kuri_-
Kuri_-OP2y ago
Thank you for replying. Yes! That's the question I'm asking. When I change it to 16, I get this:
Kuri_-
Kuri_-OP2y ago
Kuri_-
Kuri_-OP2y ago
phaseshift
phaseshift2y ago
Because you're using n-1 in your for loop i=0; i<n That's what 99% of loops should be
Kuri_-
Kuri_-OP2y ago
Kuri_-
Kuri_-OP2y ago
This is good to know.
phaseshift
phaseshift2y ago
'For' loops, that is
Kuri_-
Kuri_-OP2y ago
I still get a missing piece though. hmmmm
phaseshift
phaseshift2y ago
Most loops in c# going to be foreach You'll have to debug to investigate further
Kuri_-
Kuri_-OP2y ago
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!
Kuri_-
Kuri_-OP2y ago
Kuri_-
Kuri_-OP2y ago
It was another function. i < xxx.Length - 1 No bueno. 🙏 Thank you! 🙏
phaseshift
phaseshift2y ago
👍🏻
Kuri_-
Kuri_-OP2y ago
Woowoo!
Want results from more Discord servers?
Add your server