Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to get away from having to hard code in the names of objects I want my code to use.

For example, I'm use to do it this way:

    TextAsset test = new TextAsset();
    test = (TextAsset)Resources.Load("test.txt", typeof(TextAsset));

What I want to know, is there a way to have so that when I drag my test.txt file onto my object in Unity, my code automatically gets the name of that object?

I'm wanting to do this so once I write the code, I don't need to back in and change it should I wish re-use it.

share|improve this question

2 Answers 2

Yes, if you have a GameObject in the scene you can make a public field of the type TextAsset that can be changed in the editor.

For example, this is an array of TextAssets:

class ItemReader : MonoBehavior
{
    public TextAsset[] DataFiles;
}

After attaching the containing script to an object in the scene, the result looks like this: enter image description here

share|improve this answer

You cant use Resources.Load without using a string, i dont think that Resources accept any overload arguments.

Have u tried to define a public variable (so you can assign your object via editor) and than use it instead of strings?

Something like:

using UnityEngine; 
using System;

public class test : MonoBehaviour {     
public TextAsset tx;


    void Start()    
    {       
     Debug.Log(tx.name);    } 
    }
share|improve this answer
    
Unfortunately this does not give you the file extension or the full path, but if you are just loading the file in as a text asset this method should totally work. –  Benjamin Danger Johnson Jun 3 '13 at 16:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.