- Home /
Random placement of jigsaw puzzle
Hello, I'm trying to make a jigsaw puzzle game, here is my code for the random placement. It keep telling me that Array index out of range, i'm not sure what's wrong with the code. Any help is welcome
Thanks
PS: here is the code
public void RandomizePlacement() { //Debug.Log ("RandomizePlacement");
VectorInt2[] puzzleLocation = new VectorInt2[2];
Vector2[] puzzleOffset = new Vector2[2];
do
{
for (int i = 0; i < m_randomPases; i++)
{
puzzleLocation [0].x = Random.Range (0, (m_Size));
puzzleLocation [0].y = Random.Range (0, (m_Size));
puzzleLocation [1].x = Random.Range (0, (m_Size));
puzzleLocation [1].y = Random.Range (0, (m_Size));
puzzleOffset [0] = m_puzzle [puzzleLocation [0].x, puzzleLocation [0].y].GetImageOffset ();
puzzleOffset [1] = m_puzzle [puzzleLocation [1].x, puzzleLocation [1].y].GetImageOffset ();
m_puzzle [puzzleLocation [0].x, puzzleLocation [0].y].AssignImage (puzzleOffset [1]);
m_puzzle [puzzleLocation [1].x, puzzleLocation [1].y].AssignImage (puzzleOffset [0]);
}
} while (CheckBoard() == true);
}
An actual error message from Unity Console usually helps but in this case along with message you need to also provide exact line where error is thrown.
Just one quick guess is to check the value of your m_randomPases variable if it exceeds the length of your puzzleLocation array.
Thanks for your suggestion, finally found out, one of the object not receive the correct assign.now it's no more bug but my jigsaw not show up, will investigate more about it.
Your answer
Follow this Question
Related Questions
Unity: Switch position between quads 0 Answers
Puzzle open door ? 1 Answer
Question about puzzle game checking for other objects 1 Answer
Dynamically Slicing Images For a Jigsaw Puzzle 0 Answers
Puzzle piece rotation 0 Answers