how to switch every 4th item? added

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;
            }

        }

    }
}

Ok, I don’t know what represents what in your code. You define what’s a container, what’s an item, etc.

Now if you want to know if an index is one of every 4th, you can modulo by 4 and check if equal to 0.

I notice you have this in your code:

if (i % 9 == 4)

Not sure if this is you attempting to do that… or if it’s something else.

But this code literally says ā€œIf the remainder of i / 9 is 4, then doā€ā€¦ which is a weird thing to ask?

if (i % 9 == 4)

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.

ok…

just pointing out, that’s not every 4th…

I mean… if you want to say:
4,13,22,31,40…

Is ā€œevery 4thā€ā€¦ sure, you can say that?

Otherwise… nope, not every 4th.

That’s simple math mate.

[edit]

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.

Welp, ok.

Hopefully you realized your mistake.

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:

1 Like

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

delete this thread. i have solved my problem and no longer require assistence

That’s not how forums work…

We keep them around for future assistance if other people search for a similar question to yours.

1 Like

NO :smile:
haha kidding. i am just used to say delete for a thread no reason. and i dont know why i keep doing it