so i have been trying to create a dynamic sort of menu structure, and i want every one of these Containers to only hold 4 items then it should switch to then next container if it exist.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameSelectorScript : MonoBehaviour
{
[SerializeField]
private Canvas Canvas;
[SerializeField]
private GameObject _ScrollPanel;
[SerializeField]
private GameObject _GamePanel;
[SerializeField]
private GameObject _GameSlot;
[SerializeField]
private List<GameObject> _lGamePanel;
[SerializeField]
private List<GameObject> _lGameSlot;
[SerializeField]
private List<LevelBuilder> _Levels;
private void Awake()
{
_lGamePanel = new List<GameObject>();
_lGameSlot = new List<GameObject>();
_Levels = new List<LevelBuilder>();
_Levels.Add(new LevelBuilder());
_Levels.Add(new LevelBuilder());
_Levels.Add(new LevelBuilder());
_Levels.Add(new LevelBuilder());
_Levels.Add(new LevelBuilder());
_Levels.Add(new LevelBuilder());
_Levels.Add(new LevelBuilder());
_Levels.Add(new LevelBuilder());
CreateGamePanel();
CreateGameSlot();
}
public void CreateGameSlot()
{
for (int i = 0; i < _Levels.Count; i++)
{
_lGameSlot.Add(Instantiate(_GameSlot) as GameObject);
if (i % 9 == 4)
{
_lGamePanel.Add(Instantiate(_GamePanel) as GameObject);
for (int s = 0; s < _lGamePanel.Count; s++)
{
_lGamePanel[s].transform.SetParent(_ScrollPanel.transform, false);
}
}
}
for (int g = 0; g < _lGameSlot.Count; g++)
{
for (int i = 0; i < _lGamePanel.Count; i++)
{
Debug.Log(g);
_lGameSlot[g].transform.SetParent(_lGamePanel[i].transform, false);
}
}
}
public void CreateGamePanel()
{
if (_lGamePanel.Count == 0)
{
_lGamePanel.Add(Instantiate(_GamePanel) as GameObject);
foreach (var item in _lGamePanel)
{
item.transform.SetParent(_ScrollPanel.transform, false);
break;
}
}
}
}
i check every time i add something and makes sure that every 4th item. i add a new container. but my problem is that i want to switch the container every 4th item i add to the specific container
ok⦠i % 9 == 4 does not check if itās every 4th item.
Itād be⦠the 4th, 13th, 22nd, 31st, etc item.
i % 4 == 0
Thatād check if it was the 4th item, well the 0, 4, 8, 12, 16, etc (it includes 0, since 0 / 4 has a remainder of 0)
Modulo (%) specifically is an operator that for x % y, you get the remainder of x / y.
And if you want to ensure that on the 4th you both create the container, and use that container. Just have a variable for the current container, and when you create the next⦠you set that ācurrent containerā variable to the next.
Hrmm⦠some reason my post double posted⦠deleted one. Then noticed your post saying that i % 9 == 4 is every 4th item and including the condescending āthatās simple math mateā comment was deleted.
i got a headache to think about this right now. and my question wasnt what u answered sadly.
i asked how do i go about changing the container i use every 4 item added to the current container. and or should i instead create a list/array for each one and just put the 4 items in that?
Well I attempted to touch on that, but also pointed out that your code is sort of difficult to make heads or tails of. And that you donāt identify what are ācontainersā in your code, or āitemsā in your code.
Help link your words to your code for us, so we can follow along.
But hereās a simple example of what Iād do if I was having to do it⦠note Iāll identify what is what:
Transform containerPrefab = /*The Prefab We'll Create Containers From*/;
Transform itemPrefab = /*The Prefab We'll Create Items From*/;
int totalCount = 20; //the number of items to generate
int maxItemsPerContainer = 4; //number of items to put in each container
Transform container;
for(int i = 0; i < totalCount; i++)
{
int localIndex = i % maxItemsPerContainer; //used to get if every 4th, as well as used for things like relative spacing in the container
if(localIndex == 0)
{
//if localIndex == 0, then we've reached the 'next container', store that new one in 'container'
container = Instantiate(containerPrefab) as Transform;
/*set initial values like position */
/*note that because 0 % 4 == 0, this will create our first container as well*/
}
var item = Instantiate(itemPrefab) as Transform;
item.parent = container;
/*set initial values like position, example: */
var spacing = new Vector3(0f, 2f, 0f); //lets say items are spaced by 2 units vertically in local space
item.localPosition = (float)localIndex * spacing;
}
Iāll now suggest you consider something in my signature:
ye it sort of something like that.
i have never been able to discribe my problems to other people very well. and this thing was a rush creation, anyhow i remember that til next time.
would it be a solution for me to add in a new array that only contains 4 items ? and put those 4 items in the array foreach new container?, do bare in mind that i will at most have 4 containers having 4 items each