Question by
TheGuardian98 · May 21, 2017 at 09:24 AM ·
c#scenevariablefile-io
Reading from file (C#) + Information from scene to scene (C#)
I'm creating a C# script to load some information about a level, but I'm having some problems with it. 1) The script doesn't want to open the info.txt file; 2) I don't know how to transport a variable from a scene to another; 3) I don't know how to edit the content of the elements inside a Canvas. This i the code that I've created
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
public class LoadInformations : MonoBehaviour {
public Canvas canvas;
private string filePath = "/Files/info.txt";
void Start () {
bool i = true;
string levelC = PlayerPrefs.GetString("levelC");
try
{
string line;
StreamReader sr = new StreamReader(filePath, Encoding.Default);
using (sr)
{
line = sr.ReadLine();
do
{
if (line != null)
{
string[] words = line.Split(',');
if (words.Length > 0) {
if (words[0]==levelC)
{
Canvas.FindObjectOfType<Button>().gameObject.GetComponent<Text>().text = words[0]+" - "+words[1];
Canvas.FindObjectOfType<Text>().text = words[2];
Canvas.FindObjectOfType<Text>().text = words[3];
Canvas.FindObjectOfType<Text>().text = words[4];
Canvas.FindObjectOfType<Text>().text = words[5];
i = false;
}
}
}
line = sr.ReadLine();
}
while (line == null && i);
sr.Close();
}
}
catch (IOException e)
{
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to update variable based on scene 0 Answers
Custom inspector. Pick one bool from a list. 1 Answer
function based on return value 2 Answers
Why is my referenced variable changing values? 1 Answer
Web Game Database 1 Answer