• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by am_012 · Aug 17, 2020 at 08:36 PM · fileresourcesstartvoidcsv

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));
         }
     }
 }

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SamKim99 · Aug 18, 2020 at 07:46 AM 0
Share

Did you call the ScreenPosition with new keyword? Such that

var screenPosition = new ScreenPosition(); ...

Otherwise, i cannot find any problem.

avatar image Oribow SamKim99 · Aug 18, 2020 at 11:14 AM 0
Share

That would result in a different error message

avatar image Oribow · Aug 18, 2020 at 11:13 AM 1
Share

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?

avatar image Bunny83 Oribow · Aug 18, 2020 at 12:13 PM 0
Share

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.

avatar image am_012 Bunny83 · Aug 18, 2020 at 04:11 PM 0
Share

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)

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

136 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Avoid function being called every frame (on Start void) 1 Answer

How can I unload a single resource? 2 Answers

A Script error With Void Start 2 Answers

Uploading files at runtime and use them in a built game 1 Answer

start called directly after instantiate? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges