Search Unity

Print out list on the screen

Discussion in 'Scripting' started by organicoder, Apr 12, 2012.

  1. organicoder

    organicoder

    Joined:
    Jul 20, 2011
    Posts:
    10
    I have made the script below, which prints out just fine to the console, but what do I do to print the list on the screen when pressing a button?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class CreateGoal : MonoBehaviour {
    7.        
    8.     public List<string> stringList = new List<string>();
    9.        public string stringToEdit = "Define your goal here";
    10.    
    11.     void OnGUI()
    12.     {
    13.          stringToEdit = GUI.TextField(new Rect(10, 50, 210, 21), stringToEdit, 100);
    14.        
    15.         if (GUI.Button(new Rect(10, 80, 210, 21), "Add")){
    16.            
    17.             stringList.Add(stringToEdit);
    18.         }
    19.        
    20.         if (GUI.Button(new Rect(100, 110, 210, 21), "Debug List to console")){
    21.         DebugList();   
    22.         }
    23.        
    24.    
    25.     }
    26.    
    27.    
    28.     void DebugList(){
    29.        
    30.         foreach(string s in stringList){
    31.             Debug.Log(s);  
    32.         }
    33.            
    34.     }
    35.    
    36. }
    37.  
     
  2. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    Iterate through the strings in the list and create Labels for each one, changing the coordinates appropriately.
     
  3. organicoder

    organicoder

    Joined:
    Jul 20, 2011
    Posts:
    10
    I have now tried to do, what you suggested, but there are something that does´nt work.

    What am I missing?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6.  
    7.  
    8. public class CreateGoal : MonoBehaviour {
    9.    
    10.     int PrintLabelPosition = 200;
    11.        
    12.     public List<string> stringList = new List<string>();
    13.        public string stringToEdit = "Define your goal here";
    14.    
    15.     void OnGUI()
    16.     {
    17.          stringToEdit = GUI.TextField(new Rect(10, 50, 210, 21), stringToEdit, 100);
    18.        
    19.         if (GUI.Button(new Rect(10, 80, 210, 21), "Add")){
    20.            
    21.             stringList.Add(stringToEdit);
    22.         }
    23.        
    24.         if (GUI.Button(new Rect(100, 110, 210, 21), "Debug List to console")){
    25.         DebugList();   
    26.         }
    27.        
    28.         //Button for printing out the strings in the list
    29.             if (GUI.Button(new Rect(100, 140, 210, 21), "Print list to screen")){
    30.         PrintList();   
    31.         }
    32.        
    33.    
    34.     }
    35.    
    36.    
    37.     void DebugList(){
    38.        
    39.         foreach(string s in stringList){
    40.             Debug.Log(s);  
    41.         }
    42.        
    43.         //Function that prints out the strings in the list
    44.         void PrintList(){
    45.        
    46.         foreach(string s in stringList){
    47.             GUI.Label(new Rect(10, PrintLabelPosition, 200, 20), s);   
    48.                
    49.                
    50.                 PrintLabelPosition = PrintLabelPosition + 30;
    51.         }
    52.            
    53.     }
    54.    
    55. }
    56.