How can I read text files to determine transparency.

I have 64000 text files in my Resources folder named Wave_Position_### with the numbers corresponding to the position of the object I put the script on. I want to change the transparency based on the text files. Unity freezes when I try to run the code. This exact code used to work. I don’t know what to do.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.IO;
using System.Security.Cryptography;
using System;

public class Wave : MonoBehaviour
{
    public string[] lines;
    public string myFile;
    public Color color;
    public Renderer rend;
    string filePath, fileName;

    // Start is called before the first frame update
    void Start()
    {
        fileName = $"Wave_Position_{4*transform.position.x}_{4*transform.position.y}_{4*transform.position.z}";
        // filePath = Application.dataPath + "/Wave_Position/" + fileName;
        myFile = Resources.Load<TextAsset>(fileName).text;
        lines = myFile.Split(',');
        // lines = File.ReadAllLines(filePath).ToList();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("space"))
        {
            StartCoroutine(Animation());
        }
    }

    public IEnumerator Animation()
    {
        for (int i = 0; i <= 99; i++)
        {
            rend = GetComponent<Renderer>();
            rend.enabled = false;
            color = this.GetComponent<MeshRenderer>().material.color;
            if (float.Parse(lines*) <= 0)*

{
rend.enabled = false;
}
else
{
rend.enabled = true;
color.a = 5float.Parse(lines);*
}
this.GetComponent().material.color = color;
yield return null; // new WaitForSeconds(1f);
}
}
}

How many instances of your Wave class do you have in your scene? The way you use the coordinates and since you mentioned that you have 64k text assets lets us assume that you have probably way too many objects and each object will try to open a file named like the object position. Note that the gameobject’s position is a Vector3 and contains floating point numbers. If you have 64000 files with integer coordinates, you can only cover and area of 10x10x10 (so (-5,-5,-5) or (5, 5, 5) or (0,0,0) to (10,10,10)). However the way you create your assetname will most likely fail for some positions due to floating point rounding issues. So you should first pay more attention to your asset name generation and use proper rounding.

Having 64k gameobjects with such a script would be horrible slow. Doing 64k file access and read operations is even slower.

There are many other dangerous things in your code which are not related to performance but code stability. You hardcoded an array length of 100 elements in your loop but you read the elements from the text file. You should use the array length instead.

I have no idea why you would use 64000 text files just to set the transparency of objects, however, try using the StreamReader class from using System.IO. It reads super large files a lot better than File.ReadAllLines and I think that your program is just taking a really long time to read the files.

For example:

StreamReader reader = new StreamReader("fileDirectory");

string line;
// Reads through entire text file line-by-line
while ((line = reader.ReadLine()) != null)
{
    Debug.Log(line);
}

@rbatelaan2