- Home /
 
 
               Question by 
               astrocoder · Feb 11, 2016 at 02:01 PM · 
                c#for-loopforeachc# tutorialimplementation  
              
 
              ScrollableList best way to implement
Hello, This morning I was watching a tutorial on "UI Objects Created at Runtime" but the code that the tutorial has a feature where for-loop in which I do not see no way to adjust to my foreach code.
ScrollableList Class:
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 
 public class ScrollableList : MonoBehaviour
 {
     public GameObject itemPrefab;
     public int itemCount = 10, columnCount = 1;
 
     void Start()
     {
         RectTransform rowRectTransform = itemPrefab.GetComponent<RectTransform>();
         RectTransform containerRectTransform = gameObject.GetComponent<RectTransform>();
 
         //calculate the width and height of each child item.
         float width = containerRectTransform.rect.width / columnCount;
         float ratio = width / rowRectTransform.rect.width;
         float height = rowRectTransform.rect.height * ratio;
         int rowCount = itemCount / columnCount;
         if (itemCount % rowCount > 0)
             rowCount++;
 
         //adjust the height of the container so that it will just barely fit all its children
         float scrollHeight = height * rowCount;
         containerRectTransform.offsetMin = new Vector2(containerRectTransform.offsetMin.x, -scrollHeight / 2);
         containerRectTransform.offsetMax = new Vector2(containerRectTransform.offsetMax.x, scrollHeight / 2);
 
         int j = 0;
         for (int i = 0; i < itemCount; i++)
         {
             //this is used instead of a double for loop because itemCount may not fit perfectly into the rows/columns
             if (i % columnCount == 0)
                 j++;
 
             //create a new item, name it, and set the parent
             GameObject newItem = Instantiate(itemPrefab) as GameObject;
             newItem.name = gameObject.name + " item at (" + i + "," + j + ")";
             newItem.transform.parent = gameObject.transform;
 
             //move and size the new item
             RectTransform rectTransform = newItem.GetComponent<RectTransform>();
 
             float x = -containerRectTransform.rect.width / 2 + width * (i % columnCount);
             float y = containerRectTransform.rect.height / 2 - height * j;
             rectTransform.offsetMin = new Vector2(x, y);
 
             x = rectTransform.offsetMin.x + width;
             y = rectTransform.offsetMin.y + height;
             rectTransform.offsetMax = new Vector2(x, y);
         }
 
 
     }
 
 }
 
               My class:
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 using UnityEngine.Events;
 
 public class ShopHandler : MonoBehaviour {
 
     int counter;
     public bool isShopSelected = false;
     public PlayerHandler player;
    
     public GameObject[] itemButtons;
 
     public Item[] shopItems;
 
     public GameObject button;
 
     // Use this for initialization
     void Start () {
         itemButtons = new GameObject[shopItems.Length];
 
         counter = 0;
         foreach(Item i in shopItems) {
             GameObject btn = (GameObject) Instantiate(button);
             ItemScript scp = btn.GetComponent<ItemScript>();
 
             itemButtons[counter] = btn;
 
             scp.name.text = i.Name;
             scp.cost.text = "Cost: " + i.Cost.ToString("F3");
             scp.gain.text = "CPS: " + i.Gain.ToString("F1");
             scp.type.text = i.type.ToString();
             scp.level.text = i.Number.ToString();
             scp.icon.sprite = i.Icon;
 
             Item itemBought = i;
 
             scp.thisButton.onClick.AddListener(() => Purchase(itemBought));
             //scp.thisButton.onClick = i.btnClicked;
             btn.transform.SetParent(this.transform);
 
             counter++;
         }
 
         
     }
     
     void Purchase(Item bought) {
         
         if(bought.type == Item.Type.Console) {
            
         } else if(bought.type == Item.Type.Upgrade) {
             if(bought.Number == 10) {
                 return;
             } else {
                 bought.Number++;
             }
 
             bought.Cost *= 1.3f;
         }
        
     }
 
     void UpdateItems() {
         counter = 0;
         foreach(Item i in shopItems) {
             
             ItemScript scp = itemButtons[counter].GetComponent<ItemScript>();
 
             
 
             scp.name.text = i.Name;
             scp.cost.text = "Cost: " + i.Cost.ToString("F1");
             scp.gain.text = "CPS: " + i.Gain.ToString("F1");
             scp.type.text = i.type.ToString();
             if(i.Number == 10) {
                 scp.level.text = "✔";
             } else {
                 scp.level.text = i.Number.ToString();
             }
             scp.icon.sprite = i.Icon;
 
             
 
             counter++;
         }
     }
 
     // Update is called once per frame
     void Update () {
         UpdateItems();
 
         counter = 0;
         foreach(Item i in shopItems) {
             if(i.Cost > player.totalConsoles) {
                 itemButtons[counter].GetComponent<Button>().interactable = false;
             } else {
                 itemButtons[counter].GetComponent<Button>().interactable = true;
             }
 
             counter++;
         }
     }
 }
 
               *Im a little noob on c# language
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
Foreach Statement Random Sequence/Order? 3 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
foreach (or for-next loop) not updating local values 1 Answer
Converting foreach touch into for. 1 Answer