Noob problem with C# in unity
So this is my first time using c# and I am watching a youtube video which is where I got the code from but I typed it word for word any time I try and run it I get these errors I am brand new to unity and c# so any help would be great
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
public class DialougeParse : MonoBehaviour {
List<DialougeLine> lines;
struct DialougeLine {
string name;
string content;
int pose;
public DialougeLine(string n, string c, int p)
{
name = n;
content = c;
pose = p;
}
}
// Use this for initialization
void Start () {
string file = "Dialouge";
string sceneNum = EditorApplication.currentScene;
sceneNum = Regex.Replace(sceneNum, "[^0-9]", "");
file += sceneNum;
file += ".txt";
LoadDialouge(file);
}
// Update is called once per frame
void Update () {
}
void LoadDialouge(string filename)
{
string file = "Assets/Resources/" + filename;
string line;
StreamReader r = new StreamReader(file);
using(r)
{
do
{
line = r.ReadLine();
if(line !=null)
{
string[] line_vaulues = line.Split(",);
DialougeLine line_entry = new DialougeLine(line_values[0], line_values[1], int.Parse (line_values[2]));
lines.Add(line_entry);
}
}
while (line != null);
r.Close
}
}
}
Answer by unity-huunity · Dec 29, 2015 at 08:12 AM
line 54 you forgot " symbol
string[] line_vaulues = line.Split(","); // - change to this
Answer by Dragonlord3989 · Dec 29, 2015 at 08:21 AM
Thanks for that but now I am getting a error at line 61,20: error cs8025: Parasing error
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
public class DialougeParse : MonoBehaviour {
List<DialougeLine> lines;
struct DialougeLine {
string name;
string content;
int pose;
public DialougeLine(string n, string c, int p)
{
name = n;
content = c;
pose = p;
}
}
// Use this for initialization
void Start () {
string file = "Dialouge";
string sceneNum = EditorApplication.currentScene;
sceneNum = Regex.Replace(sceneNum, "[^0-9]", "");
file += sceneNum;
file += ".txt";
LoadDialouge(file);
}
// Update is called once per frame
void Update () {
}
void LoadDialouge(string filename)
{
string file = "Assets/Resources/" + filename;
string line;
StreamReader r = new StreamReader(file);
using(r)
{
do
{
line = r.ReadLine();
if(line !=null)
{
string[] line_vaulues = line.Split(",");
DialougeLine line_entry = new DialougeLine(line_values[0], line_values[1], int.Parse (line_values[2]));
lines.Add(line_entry);
}
}
while (line != null);
r.Close();
Ok so this is what I changed it to but it caused more errors while (line != null); r.Close(); } } }
Answer by A_Li_N · Dec 30, 2015 at 05:29 AM
Either you didn't copy your entire class into that code snippet or you are missing quite a bit of code in your class :)
C# is done in blocks of code surrounded with { and }. So you have:
void LoadDialouge(string filename)
{
string file = "Assets/Resources/" + filename;
string line;
StreamReader r = new StreamReader(file);
using(r)
{
do
{
line = r.ReadLine();
if(line !=null)
{
string[] line_vaulues = line.Split(",");
DialougeLine line_entry = new DialougeLine(line_values[0], line_values[1], int.Parse (line_values[2]));
lines.Add(line_entry);
}
}
while (line != null);
r.Close();
You see that LoadDialog opens with a {, then 'using' opens with a {, then 'do' opens with a {. You close the 'do' on line 58 but do not close the 'using', LoadDialog or the class itself. That is where the suggestion of adding }}} comes in. Adding those close the 'using', LoadDialog and class blocks of code.
Regarding further errors, it's best to post them so we know what you are experiencing. My quick review shows your line 54 has 'line_vaulues' (extra 'u') where line 55 is looking for 'line_values'.
Also, line 54: line.Split(",")... Split requires a character, not a string. Try changing those double quotes to single quotes: line.Split(',');