- Home /
Can not load csv file
I am trying to read a csv file in unity. I have my csv file in resources and was told using the void Start ()
and TextAsset region_data = Resources.Load("region_data"); would work
however I get the following error, UnityException: Load is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'ScreenPosition' on game object 'objectCreator'.
I do not know how to fix this even though I do have start function here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScreenPosition : MonoBehaviour
{
public float distance = 10f;
public float distanceChange = 1f;
public float rotationAmount = 0f;
public float rotationDelta = 0.0f;
float posX = -1f;
float posY = -1f;
float posZ = -1f;
void Start () {
TextAsset region_data = Resources.Load<TextAsset>("region_data");
}
private void Update()
{
if (Input.GetMouseButtonDown(0) || Input.GetMouseButton(1))
{
distance += distanceChange;
Vector3 clickPosition = new Vector3(posX, posY, posZ);
clickPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0f, 0f, distance));
Debug.Log(clickPosition);
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = clickPosition;
//rotationAmount += rotationDelta;
//cylinder.transform.Rotate(new Vector3(rotationAmount, 0f, 0f));
}
}
}
Did you call the ScreenPosition with new keyword? Such that
var screenPosition = new ScreenPosition(); ...
Otherwise, i cannot find any problem.
Wierd. Is that really the full unaltered code you used? I understand what the error means, but that just isn't happening in this script. Are the ** intentional?
Right. This error is completely unrelated to the posted code. He clearly uses Resources.Load from either a $$anonymous$$onoBehaviour constructor or a field initializer. Something like
public class ScreenPosition : $$anonymous$$onoBehaviour
{
TextAsset region_data = Resources.Load<TextAsset>("region_data");
// [ ... ]
}
which does not work because field initializers (and the constructor) are called from the loading thread where you can not use Resources.Load. Like the error suggests you should place the loading inside Awake or Start
public class ScreenPosition : $$anonymous$$onoBehaviour
{
TextAsset region_data;
void Start()
{
region_data = Resources.Load<TextAsset>("region_data");
}
// [ ... ]
}
However since the code in the question doesn't seem to have this issue, it's most likely not the code that produces this error.
I made a new CSV file to try again and now I get a new error when I try to run and get the lines on the file.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class readcsv : $$anonymous$$onoBehaviour { // Start is called before the first frame update void Start() {
TextAsset questdata = Resources.Load<TextAsset>("region_data");
string [] data = questdata.text.Split(new char[] { '\n' });
Debug.Log(data.Length);
}
}
I get this error
NullReferenceException: Object reference not set to an instance of an object readcsv.Start () (at Assets/Scripts/readcsv.cs:12)