Using info declared in text file

now im able to read the info in my text file, but can any 1 tell me how can i use the infomation in my text file to change the position of my character?

this is in my text file

pos x = -4.2
pos y = 0.4
pos z = -5.9

this is my c# code

using UnityEngine;
using System;
using System.IO;

class textEditor : MonoBehaviour
{
	string path = @"C:\Desktop\textcreatingTest\Assets\MyTXT.txt";
  public string fileName = "MyTXT.txt";
	
	public GameObject player;

  void Update() 
  {
    if (!File.Exists(path)) 
    {
      Debug.Log(fileName + " does not exist.");
      return;
    }
    
    using (StreamReader sr = File.OpenText(path))
    {
      String fileData;
      fileData= sr.ReadToEnd();
      sr.Close();
			
      Debug.Log("Data Read: " + fileData);
    }
  }
}

[edit] added code tags

I’ma add to this- if any if you ever modded the GTA series on PC, then you’ll know you get .dat files- practically tables of data for each thing. Just wondering if that is feasable.

your doing some things wrong.

The update loop is called every update (wich is going fast and gets called allot), so everything is execute every frame…

So change all text reading and convert it into a function.
call that function from the “Start” function.

Then you can try to extract the data from your textfile, can try the following:

  1. Use regular expressions : C# Regular Expressions Cheat Sheet
  2. use substring : String.Substring Method (System) | Microsoft Learn, then float.TryParse

then you have some float and you can create a new vector3

transform.position = new Vector3(floatX, floatY,floatY);

One way is to read your string a line at a time, and split each line into tokens. If you tokenise using spaces the 4th token will be the value you are after (after converting to a float).

Google is your friend.

Moving to Scripting.

I also added “code” tags to your post so it is easier to read. Please use around any code pasted in a post to make it readable.

SchoolProject!

Welcome to the Unity3D forums. If you need answers, try the custom search engine in my signature. For additional learning resources check out the links below. If you can’t find what you need, drop me a line and I will try to point you in the correct direction.

For your particular issue, don’t forget “TextAsset”:

With this you can simply declare a public variable and drag the asset in the inspector to make a reference.

public TextAsset myTextAsset;

Then you can access the text with myTextAsset.text.

As stated above, you can manipulate this in Awake() or Start():

public TextAsset myTextAsset
private String myString;

void Awake () {
  myString = myTextAsset.text;
  DoSomethingWith (myString);
}