Search Unity

Trying to set text component of a GameObject as it is instantiated

Discussion in 'Scripting' started by Taszty, Sep 26, 2022.

  1. Taszty

    Taszty

    Joined:
    Sep 20, 2021
    Posts:
    3
    I am currently trying to create a function that works with a StreamReader to read a line of text, parse the text, and then instantiate a text variable prefab to immediately assign that text for each line within the text file.

    I want to use the values that are split into the temps[] array as the values assigned to the text prefabs.
    I don't know how to assign these values and any help or ideas would be greatly appreciated.

    Edit: The text input lines look something like this:
    Coke,24.99,16
    Sprite,23.99,18

    Code (CSharp):
    1.  public void displayTable()
    2.     {
    3.         // temp variables
    4.         string[] temps;
    5.  
    6.         // count lines
    7.         var enumLines = File.ReadLines(saveDataPath, Encoding.UTF8);
    8.         // have reader read the lines within save file
    9.         try
    10.         {
    11.             // instantiate reader
    12.             using (StreamReader sr = new StreamReader(saveDataPath))
    13.             {
    14.                 // iterate over lines
    15.                 string line;
    16.                 while ((line = sr.ReadLine()) != null)
    17.                 {
    18.                     // parse lines into variables
    19.                     foreach(var lines in enumLines)
    20.                     {
    21.                         Debug.Log(lines);
    22.                         temps = lines.Split(',');
    23.                         // duplicate objects for use in table
    24.                         Instantiate(pfItem, new Vector3(0, 0, 0), Quaternion.identity);
    25.                         Instantiate(pfOrderCost, new Vector3(0, 0, 0), Quaternion.identity);
    26.                         Instantiate(pfOrderQuant, new Vector3(0, 0, 0), Quaternion.identity);
    27.                     }
    28.                 }
    29.             }
    30.         }
    31.         catch (Exception e)
    32.         {
    33.             Console.WriteLine("The file could not be read:");
    34.             Console.WriteLine(e.Message);
    35.         }
    36.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    GameObjects can only have a .name field, which is only visible in inspector.

    If you're seeing something onscreen, it is NOT going to be the .name field.

    If you're seeing something onscreen, it is likely a UI thing, such as a TextMeshProUGUI for instance.

    Those are just Components. You need a reference to them, then go review the documentation for the name of the field you want, usually just
    .text
    in most cases.

    Otherwise, start with tutorials for how to use the UI components or else your chances of success in this endeavor are almost zero. It's a large complicated system that needs to be grasped in an orderly fashion.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    Please use the Scripting forum for general scripting posts and not the 2D forum which is for 2D specific posts.

    I'll move your post.

    Thanks.