- Home /
Move a Camera with a Data from a Text file after Build.
Hello.
I try to move camera with a data from a text file after build. there is a text file which is refreshed while the unity 3d program is running. And I want to read the text file and use a data in it to move a camera.
I found some information about it and made a code for testing.
using UnityEngine;
using System.Collections;
using System.IO;
public class CameraController : MonoBehaviour {
public TextAsset textFile;
string tmps;
StringReader read = null;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate (new Vector3 (0, 0, 1) * Time.deltaTime);
textFile = (TextAsset)Resources.Load ("/test.txt");
read = new StringReader (textFile.text);
if (read == null) {
GetComponent<TextMesh>().text ="not found";
} else {
tmps = read.ReadLine ();
GetComponent<TextMesh>().text = tmps;
}
}
}
There is a error: NullReferenceException: Object reference not set to an instance of an object CameraController.Update () (at Assets/Scripts/CameraController.cs:20)
I heard that this way is using text file in MyProject_Data/Resources after build. But I don't know that I understood it well.
What is the way to do this? Is this code right?
Answer by Graham-Dunnett · Jun 04, 2015 at 02:05 PM
Resources.Load
is used only at runtime to load a pre-created asset. It's not possible to read a changing file using this API call. Line 20 is presumably failing because you don't check that the Resources.Load() function has actually worked.
Thanks! then, Is there any other way to read text file after build? Is it impossible just because Unity API is limited to do that?