This is driving me crazy. I'm trying to simply increase the size of a list via script.
I've tried to set the List size before I reference it by saying
List<int> listName = new List<int>(20);
but this does not work. Nothing happens. Not even in Awake or Start.
I've tried to increase the size by doing this in a loop:
foreach(var line in table)
{
int tempInt = new int();
intList.Add(tempInt);
}
this works and my list size increases.
But my problem is that AFTER increasing this list's size I cannot reference any of the elements created at runtime. I can't reference intList[2] for example.
I've tried doing this in a normal void and IEnumerator and neither work.
All I want to do here is increase my list's size as it is needed and then reference to those list items as needed....
As simple as this problem sounds this is honestly the most annoying coding issue I have ever faced in Unity.
PLEASE help.
EDIT: Here;s the code I'm using...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System;
public class DataExtraction : MonoBehaviour
{
[Header("Tables")]
public List<DataItem> tables; // The main table that needs to be populated
[Header("Extraction Variables")]
public string webAddress = "https://old.winningform.co.za/ECP/2F200717_1.htm"; // URL that the data will be extracted from
//(note this URL will be deleted after 17 July 2020
public List<string> rawDataSplit; // Place to store each line of the raw data
public int currentTable = 0;
void Awake()
{
DataItem newDataItem = new DataItem();
tables.Add(newDataItem);
}
void Start()
{
if(webAddress != "")
{
StartCoroutine(GetRequest(webAddress));
}
}
IEnumerator GetRequest(string uri)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
{
yield return webRequest.SendWebRequest();
string[] pages = uri.Split('/');
int page = pages.Length - 1;
if (webRequest.isNetworkError)
{
Debug.Log(pages[page] + ": Error: " + webRequest.error);
}
else
{
string rawData = webRequest.downloadHandler.text;
// Splitting the data into lines and assigning it to rawDataSplit list
var linesInText = rawData.Split('\n');
foreach(var line in linesInText)
{
if(line != "")
{
rawDataSplit.Add(line);
}
}
//
// Data has been split and assigned to the rawDataSplit list
AssignTableData();
//
}
}
}
void AssignTableData()
{
foreach(var line in rawDataSplit)
{
// When the script finds this line it indicates that a new Table has been found in the text
// This will create a new table in the list and increase the currentTable number by 1 (to be used as an index number)
// The following lines of data after that will then be added to THAT table until the next table is found...
// A new table will again be created and the information will be added there instead.
if(line.Contains("<table border"))
{
currentTable++;
DataItem newDataItem = new DataItem();
tables.Add(newDataItem);
tables[currentTable].rawTableData.Add(line);
}
else // If not found automatically add information into Table index 0 (created at Awake)
{
print(tables.Count.ToString());
tables[currentTable].rawTableData.Add(line);
}
}
}
}
[System.Serializable]
public class DataItem
{
public List<string> rawTableData;
}
ERROR:
NullReferenceException: Object reference not set to an instance of an object
DataExtraction.AssignTableData () (at Assets/DataExtraction.cs:89)
DataExtraction+<GetRequest>d__6.MoveNext () (at Assets/DataExtraction.cs:63)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)