Juan Ramos
Juan Ramos

Reputation: 189

How to make a 10x10 grid of buttons in Unity efficiently

GameObject[] buttonarray = new GameObject[100];
GameObject[] textarray = new GameObject[100];

Initially I was going to individually make 100 text game objects and 100 button game objects. Then I realized that I can make an array of each. Now I'm stuck since I'm not sure how to add the buttons or text to the array. enter image description here

I want to make the grid randomize an integer onto the text on the screen and have the user click it that many times until it becomes zero. Once all of them are clicked the user wins. This is how I was able to make the grid.I made them into panels and each panel with 10 buttons.I'm new to Unity so please let me know if there is a another way I might do this in unity itself.

enter image description here

Upvotes: 2

Views: 3779

Answers (2)

zambari
zambari

Reputation: 5035

There is a GridLayout component that you can use:

enter image description here

It works using the UI/Canvas, so it might need a canvas, but you can make a world space canvas with no UI rendering objects and still use it

Upvotes: 3

itstaito
itstaito

Reputation: 41

There is a component you can add to the parent object of the buttons, if none exists create one. The name of the component is escaping me at the moment however it you just search in the component field you should find it with "grid" or "layout".

What it will do is space the elements equally to your liking.

Upvotes: 0

Related Questions