- Home /
,What is going on with this scrollable window?
I keep receiving Argument is out of range error and cannot scroll using this code, any suggestion???
Here is the code:
scrollPosition = GUI.BeginScrollView(VisibleRect, scrollPosition, InvisibleRect);
int k = 0;
for (int i = 0; i < cardDatabase.PlayerCards.Count; i++) {
for (int j = 0; j < 2; j++) {
float x = j * w * 0.2f;
float y = i * h * 0.40f;
GUI.Button (new Rect(x,y, w * 0.2f, h * 0.4f), cardDatabase.PlayerCards[k].cardTexture);
k++;
}
}
GUI.EndScrollView();,Hi, i keep receiving th error ArgumentOutOfRangeException and mos importantly i cannot scroll and see.
Here is the code:
scrollPosition = GUI.BeginScrollView(VisibleRect, scrollPosition, InvisibleRect);
int k = 0;
for (int i = 0; i < cardDatabase.PlayerCards.Count; i++) {
for (int j = 0; j < 2; j++) {
float x = j * w * 0.2f;
float y = i * h * 0.40f;
GUI.Button (new Rect(x,y, w * 0.2f, h * 0.4f), cardDatabase.PlayerCards[k].cardTexture);
k++;
}
}
GUI.EndScrollView();
Answer by Bunny83 · Dec 09, 2017 at 11:41 PM
Well, your code doesn't make much sense as your "k" will of course will go out of range. Your outer for loop with variable "i" will do "cardDatabase.PlayerCards.Count" iterations. So i basically goes through all valid indices. However inside the outer loop you have a nested loop. which does two iterations for each of your player cards. Since you increment "k" inside the inner loop your "k" doesn't go from 0 up to "cardDatabase.PlayerCards.Count-1" but to double that range.
It looks like you want to arrange the cards in two columns. However your for loop ranges doesn't work this way. Even if you set the outer loop count to half the elements you have another problem. In this case your card count has to be a multiple of 2 (2, 4, 6, 8, 10, 12, 14, ...) because if the count is an odd number your inner loop will go out of range for the last "pair" which has only one card.
It's usually easier to do the opposite. Just iterate through your cards and update the x / y position every iteration:
int numOfColumns = 2;
int x = 0;
int y = 0;
float cw = w * 0.2f;
float ch = h * 0.4f;
for (int i = 0; i < cardDatabase.PlayerCards.Count; i++) {
GUI.Button (new Rect(x * cw, y * ch, cw, ch), cardDatabase.PlayerCards[i].cardTexture);
if (++x >= numOfColumns)
{
x = 0;
y++;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612144306im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Tracking Down GUI Errors 0 Answers
Can't use any GUI functions 1 Answer
Adding score when enemy dies (Errors) 2 Answers
Error : Missing GUILayout.EndScrollView/EndVertical/EndHorizontal? UnityEditor.DockArea:OnGUI() 1 Answer
GUI Error: You are pushing more GUIClips than you are popping. Make sure they are balanced) 2 Answers