I am trying to use a C# script to access a public variable in another C# script, and to do so I am using GetComponent. It gave me no problems until a few days ago. However, now GetComponent is returning an empty variable. Not null - it has the same type that it is supposed to, and does not throw a Null object exception, it’s just…empty. I don’t know what the technical term for this is. If the variable I am trying to access is an array, it returns an array of length 0. If it is a string, it returns an empty string.
The first script in my case is called TextHandler. It is attached to a GameObject called TextHandler in my scene, and reads a CSV file one line at a time and outputs each line into a float[ ] called nums.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class TextHandler : MonoBehaviour
{
string path = "Assets/" + "test.csv";
StreamReader reader;
public float[] nums;
float[] ParseString(string str) {
string[] spl = str.Split(",");
int l = spl.Length;
float[] arr = new float[l];
for (int i = 0; i < l; i++) {
arr[i] = float.Parse(spl[i]);
}
return arr;
}
// Start is called before the first frame update
void Start()
{
reader = new StreamReader(path);
}
// Update is called once per frame
void Update()
{
//Read the text from directly from the test.csv file
string line = reader.ReadLine();
Debug.Log(line);
nums = ParseString(line);
}
}
It also prints each line to the console. It does this with no issues. I also used a loop to print each element of nums to the console, and also had no issues with that.
The second script is called TestNums, attached to another GameObject in the scene. It tries to access the variable nums in TextHandler, and print its first element to the console.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestNums : MonoBehaviour
{
TextHandler data;
float[] nums;
// Start is called before the first frame update
void Start()
{
data = GameObject.Find("TextHandler").GetComponent<TextHandler>();
}
// Update is called once per frame
void Update()
{
nums = data.nums;
Debug.Log("num: " + data.GetComponent<TextHandler>().nums[0]);
}
}
This script gives me an IndexOutOfBounds error at runtime, because nums[0] doesn’t exist. nums in the TestNums script is a float[ ] with length 0. It is supposed to have length 22.
There are no compilation errors or other runtime errors. GetComponent successfully accesses the TextHandler GameObject, the TextHandler component within that GameObject, identifies the variable within the component, and returns a variable with the same type. It just doesn’t return the right value.
Things I tried already:
- Printing nums itself to the console instead of one of its elements, to see whether GetComponent was accessing the correct variable. This gives an output of System.Single[ ], which is expected for a float[ ].
- Initializing nums as a float[22], in case the call to nums[0] happened before the variable was assigned the first time. No effect, same error.
- Making nums in TestNums public and using the Inspector to set nums as a list of 22 elements (same reason as above). No effect, same error.
- Making the variable line in TextHandler public and trying to access it instead of nums, in case the problem was just with the one variable. It returns an empty string.
I am using Unity 2022.3.5f1 on Windows 11. Does anyone know what the problem here is? Did I miss something in my code, or is there a bug in Unity?