hi, i’m new with unity and using c#
each of the floor shall contain its own data which obtained from a .txt file. so this is the outline
First, we read the schedule CSV as a series of strings
for each line split it into values (using space/tab/semicolon). The first field will give us the entity ID
Traverse the scene and compare the name of the object with the ID.
If found, add the data to the entity, using a custom component with two list of strings: keys and values.
this is what i did based on this blog Getting BIM data into Unity (Part 5 - Parsing a Schedule in Unity)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
public class Schedule_Parser : MonoBehaviour
{
//the internal file name private
private string fileToParse = "";
//some public variables to configure this script
public string filePath = "filePath";
public string fileName = "fileName";
public string fileExtension = "txt";
//declaring another 2 because want to fill into key and value as in array
public int headersLineNumber = 0;
public int valuesFromLine = 1;
// use this for initialization
void Start()
{
fileToParse = filePath;
fileToParse = Path.Combine(fileToParse, fileName);
fileToParse = fileToParse + "." + fileExtension;
FileInfo theSourceFile = null;
TextReader reader = null; // NOTE: TextReader, superclass of StreamReader and StringReader
// Read from plain text file if it exists
theSourceFile = new FileInfo(Path.Combine(Application.dataPath, fileToParse));
if (theSourceFile != null && theSourceFile.Exists)
{
reader = theSourceFile.OpenText(); // returns StreamReader
Debug.Log("Created Stream Reader for " + fileToParse + " (in Datapath)"
);
}
if (reader == null)
{
Debug.Log(fileName + " not found or not readable");
}
else
{
// Read each line from the file/resource
bool goOn = true;
int lineCounter = 0;
string[] headers = new string[0];
while (goOn)
{
string buf = reader.ReadLine();
if (buf == null)
{
goOn = false;
return;
}
else
{
Debug.Log("Current Line : " + lineCounter + " : " + buf);
string[] values;
if (lineCounter == headersLineNumber)
{
headers = buf.Split(',');
Debug.Log("--> Found header " + headers[0]);
}
if (lineCounter >= valuesFromLine)
{
// now we get a , ; or -delimited string with data
// ID ...
values = buf.Split(',');
string ID = values[0];
Debug.Log("--> Found values " + values[0]);
GameObject go;
go = GameObject.Find(ID);
if (go == null)
{
foreach (var gameObj in
FindObjectsOfType(typeof(GameObject)) as GameObject[])
{
if (gameObj.name.Contains(ID.ToString()))
{
go = gameObj;
}
}
}
if (go != null)
{
Debug.Log(" Found ID : " + ID);
go.AddComponent<Metadata>();
Metadata Meta = go.GetComponent<Metadata>();
Meta.values = values;
Meta.keys = headers;
}
else
{
Debug.Log(" No objects found with ID: " + ID);
}
}
}
lineCounter++;
}
}
}
}
I’m trying to attach each of the data from the .txt file to its object
2 script were conducted. one is metadata script containing the variable of keys and values
and the second one is the schedule parsing script, to assign the parameter and the values in the metadata.
the error that i obtained is that no object were found with the ID although both of the game object and the .txt file contain the same ID so that they are able to be relate to each other.
Is there any error that i might missed out?
the last picture is the expectation. when i hit the play button, the data retrieved by schedule_parser script will be displayed in the metadata (that I’ve created earlier as the scrip 1).