0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class SavedGamesSlots : MonoBehaviour
{
    public GameObject saveSlotPrefab;
    public float gap;

    private Transform slots;
    private string[] imagesToLoad;

    // Start is called before the first frame update
    void Start()
    {
        imagesToLoad = Directory.GetFiles(Application.dataPath + "/screenshots", "*.png");
        slots = GameObject.FindGameObjectWithTag("Slots Content").transform;

        for (int i = 0; i < imagesToLoad.Length; i++)
        {
            var go = Instantiate(saveSlotPrefab);
            go.transform.SetParent(slots);

            Texture2D thisTexture = new Texture2D(100, 100); //NOW INSIDE THE FOR LOOP
            string fileName = imagesToLoad[i];
            byte[] bytes = File.ReadAllBytes(fileName);
            thisTexture.LoadImage(bytes);
            thisTexture.name = fileName;
            GameObject ChildGameObject = go.transform.GetChild(1).gameObject;
            ChildGameObject.GetComponent<RawImage>().texture = thisTexture;
            var raw = go.GetComponent<RectTransform>();
            raw.anchoredPosition = new Vector3(1 + 1 * gap, 6, 0);


        }
    }

    // Update is called once per frame
    void Update()
    {

    }
}

The way I'm doing it now is not working it's moving all the objects to the same position and not adding equal gap's between them.

In the loop I tried to do it this way in this line :

raw.anchoredPosition = new Vector3(1 + 1 * gap, 6, 0);
\$\endgroup\$
1
  • 1
    \$\begingroup\$ but this does not contain the loop variable and is always the same value "new Vector3(1 + 1 * gap, 6, 0)" \$\endgroup\$
    – Zibelas
    Jan 26 '21 at 12:11
1
\$\begingroup\$

You need to multiply your gap value by the i of your loop. Right now, 1 + gap is a constant value, whereas i * gap will increase when i increase. If you want to offset your objects, add some constant like c + i * gap and if you want to increase the gap, well increase your gap variable.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .