Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Naim · May 04, 2011 at 03:09 AM · textasset

Using Resources.Load from a non script component class

I have a normal non-script component class (ie. does not inherit from MonoBehaviour, no Awake, Start funcs, etc) and I'm using it to parse and load text file assets, but I keep getting an error: "You are not allowed to call get_text when declaring a variable". Is there some problem using Resources.Load from outside of a Unity script?

{ StringReader reader = null;

 TextAsset textAsset = (TextAsset)Resources.Load(stringTableName, typeof(TextAsset));
 // blah, error checking

 // read the strings from file and insert into the hash table
 reader = new StringReader(textAsset.text);

The error happens on the line where I create the StringReader to parse the file. I mean, this is simply a normal class, so it can only be called from script, right? And by the time that a script component is able to create/call functions from this class, unity should be all set up and ready to go, so I'm not sure what the problem is.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Mike 3 · May 04, 2011 at 03:39 AM

No, you can call it from (almost) anywhere, the issue is that you're assigning when you're declaring it as a member field

What you need to do is assign to it in the constructor probably:

public class YourClass { TextAsset textAsset;

 public YourClass()
 {
     textAsset = (TextAsset)Resources.Load(stringTableName, typeof(TextAsset));
 }

}

This may not fix it if the class is being created in a similar way though (i.e. at someplace other than inside an event function called from unity), so at that point you may just need to put the assign into whichever functions use it, after checking if it's null, e.g.

public void Test()
{
    if (textAsset == null)
    {
        textAsset = (TextAsset)Resources.Load(stringTableName, typeof(TextAsset));
    }
}
Comment
Add comment · Show 6 · Share
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 Naim · May 05, 2011 at 07:48 AM 0
Share

Why is this exactly? I'm writing a StringTable$$anonymous$$gr class, and this is happening inside the function to parse and load a text file as a string table. The function that I'm calling it from is a static function, since the StringTable$$anonymous$$gr is going to be accessed from everywhere in code and it seemed best to implement it as a singleton.

The point is, is that since it is getting called from other unity script code, correct me if I'm wrong, but it is always going to be called somewhere down the call stack from a unity event. So I don't understand as to why this wouldn't work.

avatar image Mike 3 · May 05, 2011 at 08:19 AM 0
Share

Not always, no. Imagine something like $$anonymous$$yClass test = $$anonymous$$yClass.Instance; as a member variable to a class. That won't be called by a unity event, but will be called at some other point ins$$anonymous$$d, which can often lead to screwups

avatar image Naim · May 05, 2011 at 09:53 AM 0
Share

I'm a beginner with Unity scripting, but my impression was that each script has (at least) a Start function and an Update function (and others). Are these unity events? I mean, if my class is only accessed from these (and similar functions), unity has to call these functions, so does that make them unity events?

Or, if my class is called from a constructor of an object that is called from a unity event, does this qualify, or does unity just call it an error because it's not a sure thing?

avatar image Mike 3 · May 05, 2011 at 10:33 AM 0
Share

those are what i meant by unity events, yeah. generally you're ok if you create instances from inside of them, though still probably best to stay clear of assigning objects to member fields inside the created objects

avatar image Naim · May 05, 2011 at 11:27 AM 0
Share

Well then, what I want to do may be impossible in Unity then. But then how does this (http://forum.unity3d.com/threads/35617-Text$$anonymous$$anager-Localization-Script) work? I'm doing the same as him except that my class isn't a Unity script component (doesn't inherit from $$anonymous$$onoBehavior). Is it because he creates and adds his object to a GameObject?

Show more comments
avatar image
0

Answer by Naim · May 10, 2011 at 03:24 AM

This is a commercial project, so I don't know if I can post any of the specific code, but I've found that even when trying a cut down test class, with the exact same form, that the error doesn't occur.

The test class is:

public class TempClass { static TempClass m_TempClass; List<string> m_StringList; StringReader reader;

private static TempClass Instance { get { if (m_TempClass == null) { m_TempClass = new TempClass(); } return m_TempClass; } }

public static TempClass GetInstance() { return Instance; }

private TempClass() { m_StringList = new List<string>(); }

public static string LoadString(string fileName) { TempClass tempClass = GetInstance();

 string temp = ParseFile(fileName);

 return temp;

}

static string ParseFile(string fileName) { string temp;

 TextAsset textAsset = (TextAsset)Resources.Load(fileName, typeof(TextAsset));
 Instance.reader = new StringReader(textAsset.text);

 temp = Instance.reader.ReadLine();

 for (;
     temp != null;
     temp = Instance.reader.ReadLine())
 {
     Instance.m_StringList.Add(temp);
 }

 return temp;

} }

I don't know how much help it will be to see the code that works, but I'm hoping it sparks something. I've found that in the actual code, the error occurs during the usage of 'textAsset.text' If I replace that with a string literal, then the script compile error doesn't occur.

Comment
Add comment · Share
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

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

No one has followed this question yet.

Related Questions

Problem with StringReader or TextAsset not reading some characters 1 Answer

Automatic CSV to TXT conversion [solved] 1 Answer

XML File, or Array? 1 Answer

Trouble with Resources.Load 1 Answer

Possible to edit the text markdown to call functions? 0 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