- Home /
Public Static List Dictionary access from another Script OUT OF RANGE ERROR
I've read all answers about access to lists from another scripts....but really unable to find out the problem. What I'm trying to do : I populate a List with Dictionary, reading data from a CSV (I've found a very good function somewhere CSVReader). Debug.Log works correctly (and also CSVReader works) To optimize my code and speed ( I think ), I want to read only one time the CSV and store it in a public static list. (actually I populate some DropdownMenu reading everytime CSV, creating List ecc ecc... just a copy/paste of code)
So I found the way to call the public static list after the initialization (I use a Button to avoid early reading before creation). But even if my public static List was created and worked... It seems like disapearing and accessing to List from another script give me OutOfRange Error
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BootLoader_VideoGame : MonoBehaviour
{
[Tooltip("Inserire nome del File da cui prelevare la lista")]
private string FileName="ListeSingole";
public static List<Dictionary<string,object>> SingoleListe = new List<Dictionary<string,object>>();
// Start is called before the first frame update
void Start() {
// Legge il nome del file (deve essere in /Resource) e lo mette in una lista
List<Dictionary<string,object>> SingoleListe = CSVReader.Read (FileName);
Debug.Log("ListaSingole:"+SingoleListe[2]["CONSOLE"]);
}
}
Above I create the List and Debug.Log show me it is populated. Below I recall it...and give me OutofRange Error
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class MakeCSV : MonoBehaviour
{
[SerializeField]
private string FileNameCSV;
public GameObject BootLoader;
private string teststring;
public void CreateCSV()
{
Debug.Log("TestString :" + BootLoader_VideoGame.SingoleListe[2]["CONSOLE"]);
Not sure...but can be CSVReader function delete my list every time is called (for example from Dropdown Scripts)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DropDownScript : MonoBehaviour
{
[Tooltip("Punta al DropDown da popolare...")]
public Dropdown Maindropdown;
[Tooltip("Inserire nome del File da cui prelevare la lista")]
private string FileName="ListeSingole";
[Tooltip("Inserire nome della colonna da Popolare")]
public string ColName;
void Start () {
// Legge il nome del file (deve essere in /Resource) e lo mette in una lista
List<Dictionary<string,object>> data = CSVReader.Read (FileName);
Debug.Log("Test String : "+int.Parse((data[0][ColName]).ToString()));
//clear/remove all option item
Maindropdown.options.Clear ();
//fill the dropdown menu OptionData with all COM's Name in ports[]
for(var i=1; i < (int.Parse((data[0][ColName]).ToString())+1); i++)
{
// Popola il DropDown con il contenuto della lista
Maindropdown.options.Add (new Dropdown.OptionData((data[i][ColName]).ToString()) );
}
//Refresh the visual DdMenu
Maindropdown.RefreshShownValue();
}
}
Not really important...but I like speed and clean (even if I'm not a programmer) To resume : I only want to create a Single Public Dictionary List from a CSV and access to it every time I want and need. But after creation it can't be accessed from others scripts.
Answer by ShadyProductions · Apr 30, 2020 at 11:03 AM
void Start() {
SingoleListe = CSVReader.Read(FileName);
Debug.Log("ListaSingole:"+SingoleListe[2]["CONSOLE"]);
}
It should be like this, otherwise you are specifying a local variable. Also make sure that this script runs before the other scripts. (Script Execution Order)
I've figured out...yes it was simple when you know how to do. Thank you for your reply
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Accessing a list from another script 2 Answers
Access List from another script 0 Answers
Acces list from another script 2 Answers
C# List of Files 1 Answer