Search Unity

questiona about UI TOOLS: SCROLLING MENUS AT RUNTIME

Discussion in 'Scripting' started by tomjoe, May 29, 2015.

  1. tomjoe

    tomjoe

    Joined:
    May 11, 2015
    Posts:
    44
    Hi guys i was looking at this great tutorial
    https://unity3d.com/learn/tutorials...ing-archive/creating-scroll-lists-at-run-time
    and i wanted to make the list update during run time. so i can add and remove Items from the list and have it update.

    so i changed the void Start() to a void Update(). but now my list just loops instantiates the buttons over and over until they disappear off the screen.

    any thought??

    Code (CSharp):
    1. void Update()
    2.     {
    3.         PopulateList ();
    4.     }
    5.     void PopulateList()
    6.     {
    7.         foreach (var item in itemList)
    8.         {              
    9.                     GameObject newButton = Instantiate(itemButton) as GameObject;
    10.                     ItemButtonScript buttonScript = newButton.GetComponent<ItemButtonScript>();
    11.                     buttonScript.nameLabel.text = item.name;                  
    12.                     newButton.transform.SetParent(contentPanel);
    13.          }
    14.     }
    15.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you're calling this from Update(), update runs every frame, so every frame you add more and more and more....

    depending on what you are trying to do, you either want that code in Start (executes once at the beginning) or contain it within some sort of if statement (whatever you want to trigger the addition of new buttons).
     
  3. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    Never ever loop in a Update without some sort of condition that controls when to execute the loop.
    Essentially here you have an infinite loop = BAD.
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    How is that an infinite loop?
     
  5. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    @KelsoMRK Are you implying that is it NOT an infinite loop?
    PopulateList (and hence it's foreach loop) will be called forever and ever until the game stops , paused or the script is deactivated because the method is called in the Update without any condition.
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Right. So every frame it loops through the list and does something. What is infinite about that? The loop ends when the end of the collection is found.

    This is an infinite loop in Update
    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.     bool running = true;
    5.     while (running)
    6.     {
    7.         Debug.Log("running");
    8.     }
    9. }
    10.  
     
    Deleted User and sluice like this.
  7. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    Yeah you are right, I don't know if it's because it's Friday OR because it's my last day before my holidays OR both... but I'm easily confused today! :D
     
    Deleted User likes this.
  8. Deleted User

    Deleted User

    Guest

    It's not an infinite loop. It will only happen X number of times based on how many items are in itemList then it will stop.

    EDIT: woops should have refreshed before posting!
     
    sluice likes this.
  9. tomjoe

    tomjoe

    Joined:
    May 11, 2015
    Posts:
    44
    Alright, thanks guys, ill look in to some other updater logic.