• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by frhilyana118 · Jan 30, 2020 at 03:00 AM · parsing

attach the game object with its data from a .txt file

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 http://cad-3d.blogspot.com/2016/03/getting-bim-data-into-unity-part-5.html

 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.

alt text

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). alt text

result.png (228.7 kB)
expectation.png (36.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sacredgeometry · Jan 30, 2020 at 07:52 AM

Wow thats a lot of code to parse a csv try:


https://www.dotnetperls.com/textfieldparser

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

118 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

(solved)Pasing WWW data post/get to load gameobjects? 4 Answers

Parsing Error: 1 Answer

parsing epub file into unity? 0 Answers

Iterating through multidimensional arrays 2 Answers

Real Time Multi-Developer Level Editing 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges