Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

List does not exist in current context

Discussion in 'Scripting' started by LegendXV, Jun 2, 2015.

  1. LegendXV

    LegendXV

    Joined:
    May 31, 2015
    Posts:
    30
    I was trying to make an int list to store my values, so I defined it outside of any method and then wanted to set its length inside the start method, but I got an error (defined in the title of the thread). Here's my code ...

    Code (csharp):
    1.  
    2. public class Player : MonoBehaviour {
    3.  
    4.     private List<int>[] possibleValues;
    5.     private string currentMode = "";
    6.     private string[] playModes = {"evenToOdd", "oddToEven", "incremental"};
    7.     public Text chosenValues;
    8.  
    9.     // Use this for initialization
    10.  
    11.     public GameObject prefab;
    12.  
    13.     void Start() {
    14.         possibleValues = new List<int>[2];
    15.         for (int x = 0; x < Random.Range (6,10); x++) {
    16.             GameObject go = Instantiate(prefab, new Vector3(Random.Range (-4,4), 1, Random.Range (-4,4)), Quaternion.identity) as GameObject;
    17.             Clickable clicky = go.GetComponent<Clickable>();
    18.             clicky.initiate(this, Random.Range (0,10));
    19.         }
    20.     }
    21.  
     
  2. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    192
    List<> is part of the System.Collections.Generic namespace, make sure you have using System.Collections.Generic; in the header of your script.
     
    Kiwasi likes this.
  3. LegendXV

    LegendXV

    Joined:
    May 31, 2015
    Posts:
    30
    Do you know how to convert a list into a string connected by commas for each element? I'm having problems doing so :/
     
  4. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    192
    Using StringBuilder from System.Text, you could do something like this considering your list (possibleValues) contains integers:

    Code (csharp):
    1. System.Text.StringBuilder sb = new System.Text.StringBuilder();
    2.  
    3. // IEnumerable iteration using bool to check if we're on first value
    4. bool firstValue = true;
    5.  
    6. foreach(int _int in possibleValues)
    7. {
    8.    if(firstValue)
    9.    {
    10.      sb.Append(_int);
    11.      firstValue = false;
    12.    }
    13.    else
    14.    {
    15.      sb.Append("," + _int);
    16.    }
    17. }
    18.  
    19. // For loop grabbing values by index
    20. for(int i = 0; i < possibleValues.Count; i++)
    21. {
    22.    if(i == 0)
    23.    {
    24.      sb.Append(possibleValues[i]);
    25.    }
    26.    else
    27.    {
    28.      sb.Append("," + possibleValues[i]);
    29.    }
    30. }
    31.  
    32. Debug.Log("Final string: " + sb.ToString());
    Either one should work. Applying StringBuilder here because I read an article recently that appending strings manually can cause some excess work for the GC.

    edit: I think I read something about IEnumerable iterations as well, can't remember what exactly. If anybody reading this knows maybe clear it up for me, are IEnumerable iterations hefty towards memory management?
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    GC being "garbage collection", the process that unity does to clean up the mess you most likely never realise your making behind the scenes until your project grinds to a halt for "no reason" :D