Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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.

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

Access path of stored asset 0 Answers

parsing csv file 2 Answers

Multiple Lists to CSV in C# 0 Answers

Full list of predefined voids? 2 Answers

How can I unload a single resource? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges