Access variables from text file

How can i am using variables from text file, for example:
gametime = 60
screenwidth = 1280
screenheight = 720
I need to use this variables in other script. How can i do this?
Should i have try textasset http://docs.unity3d.com/Documentation/ScriptReference/TextAsset-text.html? But how to read data from text?

Once you read the text just split the text by “\n” into rows and then split each row by “=”, here is how you can do using TextAsset (didn’t tried the code, just thinking):

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class example : MonoBehaviour {
   public static example instance;
   public TextAsset asset;

   Dictionary<string, string> _settings = new Dictionary<string, string>();
   public Dictionary<string, string> settings { get { return _settings; } }

   void Awake () {
      instance = this;
      settings = new Dictionary<string, string>();
      string[] rows = settings.Split('\n');
      foreach (string row in rows) {
         string[] data = row.Split('=');
         settings.Add(data[0].Trim(), data[1].Trim());
      }
   }
}

So you can access to example.instance.settings[“gametime”] (if you need number values you will have to convert from string using int.Parse). This code could be extended to support string values containing “=” or more improvement, but I think it is a good start.

1 Like

It would be better for you to use a ScriptableObject like this:

public class MyData : ScriptableObject
{
	public int gametime = 60;
	public int screenwidth = 1280;
	public int screenheight = 720;
}

ScriptableObject’s are specially designed to store data.

Create asset of this script in the resource folder, and load it with Resources.Load();

1 Like

Thanks for answer.
SkaredCreations i really didnt understand how can i access my variable from textfile and i didnt know how to using int.Parse.
GaVaR i am using this code:

But i have error