- Home /
How can I make this for loop go down to the next row when done with 3?(READ FULL)
I have a for loop that makes buttons in a crate. I need it to be so that when the for loop makes 3 buttons, the buttons begin to start on the next row using the variable buttonHeight * cnt to control how far down they go. Can anyone help me out here? Heres the script. using UnityEngine; using System.Collections; using System.Collections.Generic;
public class OpenCrateScript : MonoBehaviour
{
static public bool lootCrateOpen = false;
public float maxDistance;
public Texture2D CrateWindow;
public GUIStyle CrateSlotStyle;
public GUIStyle CrateButtons;
private List<Item> lootItems;
private int buttonWidth = 78;
private int buttonHeight = 78;
void Start ()
{
maxDistance = 2.0f;
lootItems = new List<Item> ();
Populate ();
}
void Update ()
{
if (craftingMenu.craftingMenuOn == true || BuildingMenu.buildingMenuOn == true)
{
lootCrateOpen = false;
}
if (Input.GetKeyUp(KeyCode.E) && lootCrateOpen == false) {
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) {
if (hit.collider.CompareTag("Loot Crate") && Vector3.Distance(hit.collider.transform.position, transform.position) <= maxDistance) {
lootCrateOpen = true;
}
}
}
else if (Input.GetKeyUp(KeyCode.E) && lootCrateOpen == true)
{
lootCrateOpen = false;
inventorySystem.inventoryOn = false;
inventorySystem.toolbarOn = false;
}
if (inventorySystem.inventoryOn == true && Input.GetKeyUp (KeyCode.E))
{
lootCrateOpen = false;
}
}
void OnGUI()
{
if (lootCrateOpen == true)
{
GUI.BeginGroup(new Rect(760,30,600,750), CrateWindow);
for(int cnt = 0; cnt < lootItems.Count; cnt++)
{
GUI.Button (new Rect(buttonWidth * cnt,78,buttonWidth,buttonHeight),cnt.ToString(),CrateSlotStyle);
}
GUI.EndGroup();
inventorySystem.inventoryOn = true;
inventorySystem.toolbarOn = true;
}
}
void OnMouseEnter()
{
}
private void Populate()
{
for (int cnt = 0; cnt < 25; cnt++)
{
lootItems.Add (new Item());
}
}
}
Tell me if you need more information! And thanks for any help.
Answer by SirCrazyNugget · Mar 24, 2014 at 09:59 AM
You've got 2 options, use modulo or use 2 for loops
int offsetX = 100; //position for first button on X axis
int offsetY = 40; //position for first button on Y axis
int buttonWidth = 32;
int buttonHeight = 32;
modulo:
int totalButtons = 18;
int maxCols = 6;
for(int i = 0; i < totalButtons; i++){
Rect r = new Rect ((i % maxCols) * buttonWidth + offsetX, (int)i / maxCols * buttonHeight + offsetY, buttonWidth, buttonHeight);
//draw button using Rect r
}
two for loops:
int maxRows = 3;
int maxCols = 6;
int offsetX = 100; //start drawing on X axis from 100
int offsetY = 40; //start drawing on Y axis from 40
for(y = 0; y < maxRows; y++){
for(x = 0; x < maxCols; x++){
Rect r = new Rect (x * buttonWidth + offsetX, y * buttonHeight + offsetY, buttonWidth, buttonHeight);
//draw button using Rect r
}
}
Both of these should draw 18 buttons in a 6 x 3 grid
Your answer
![](https://koobas.hobune.stream/wayback/20220613141018im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
C# For loop in button to set gameObjects in array to active 1 Answer
my wave spawner not working properly 1 Answer
Getting almost nowhere with my buttondrag script 1 Answer
List of Buttons: Adding a listener passes Last element? 2 Answers
(For Loop) Converting Gui Button to GuiText & GuiTexture 1 Answer