Taking data from text file

Currently I have a TextField that accepts an input string and then in another script, this string is passed on and if statements check the string, moving the camera to a different position if the string is of the specified criteria. The problem is, I will have many different strings and matching coordinates for each one (the coordinates correspond to the camera’s transform.position). I don’t want to write a different if statement for each one.
What I am looking to do is have a single if statement that checks the string from TextField against the contents of a text file. The relevant coordinate will then be taken form the text file and passed to the transform.position of the camera object.

The text file will be in the format of String/Coordinate on each line. An example would be


Currently my script has the following code:

using UnityEngine;

using System.Collections;

class CameraPositions : MonoBehaviour {
	public SearchField Field;
	private Vector3 Entry;
	private string Result;
	private bool confirm;
	#region methods
	void Update()
		confirm = Field.Confirm;
		if(confirm == true)
		Debug.Log (transform.position);
	void UpdateResult()
		Result = Field.Search;
		if(Result ==  "England" )
			Entry = new Vector3( -87.37f,49.43f,20.22f);

	void UpdateCamera()
		GameObject CameraContainer = GameObject.FindGameObjectWithTag("Player");
		CameraContainer.transform.position = Entry;

I have looked at similar questions on here but they seem to be solved by using arrays. I’m not sure if my problem requires an array or not and do not have much experience with them.

Any help would be greatly appreciated.

Here, this should work, I tested it, but it uses arrays. Basically it reads the file at the beginning, and has a search function. Hope it works.
I tested it with the following:


And it worked


using UnityEngine;
using System.IO;
using System.Collections;

public class Coordinates : MonoBehaviour {
	public string[] Names;
	public Vector3[] Locations;
	string filepath = "./example.txt"; //This is the path of the text file
	// This gets all the locations from the file
	void Start () {
		string[] filelines = File.ReadAllLines(filepath);
		Names = new string[filelines.Length];
		Locations = new Vector3[filelines.Length];
		for(int i = 0; i < filelines.Length; i++)
			string[] parts = filelines*.Split("/"[0]);*

_ Names = parts[0];_
* string coords = parts[1].Substring(1,parts[1].Length-3);*
* string[] coord = coords.Split(“,”[0]);*
* float coord0 = float.Parse(coord[0]);*
* float coord1 = float.Parse(coord[1]);*
* float coord2 = float.Parse(coord[2]);*
_ Locations = new Vector3(coord0, coord1, coord2);
Debug.Log(Names + " : " + Locations*.ToString());*
* }

* //This finds the entry*
* //It takes one parameter, the string it is searching for*
* //If it finds it, returns the location assosiated with it*
* //Else, returns 0, 0, 0*
* Vector3 FindEntry (string findthis) {*
* for(int i = 0; i < Names.Length; i++)*
* {*
_ if(Names == findthis) //Iterates trought names and checks if the name is what we are looking for
* {
Debug.Log("Found: " + findthis + " at " + Locations.ToString());
return Locations; //Returns the location associated with it*

* }
//This is what happens if it found it*

* Debug.Log(“Unable to find the location specified”);
Vector3 result = new Vector3(0,0,0);
return result; //Returns (0, 0, 0)

Not sure what Field is about, but if you want to parse that string into something useful, here’s some nuggets:

Assume you read England/(-87.37f,49.43f,20.22f) into a string called ‘inputString’

string[] parts = inputString.Split("/"[0]);
// parts[0] now has "England" and parts[1] has "(-87.37f,49.43f,20.22f)"
string coords = parts[1].Substring(1,parts[1].Length-2); // now has "-87.37f,49.43f,20.22f"
string coord = coords.Split(","[0]);
float x = parseFloat(coord[0]);
float y = parseFloat(coord[1]);
float z = parseFloat(coord[2]);
Vector3 pos = new Vector3 (x,y,z);