Please Help. Why is Unity not loading ALL lines of my txt?

Hi,

i have 2 scripts and 1 .txt file.

One Script is a script (levelbuilder) that builds the level depending on the “levels” script.
The “Levels” Script should read all lines from a .txt File (about 10.000 character).
Every Level has about 10-15 Lines (“#” is a wall, “!” is a character, “?” is a light and so on). The Levels are seperatet with a “;” in the file.

But if i try to load about 40 Levels, every seperatet with “;” nothing displays. If i only load 9 Levels, with “,” to seperate each from another it loads fine. (1341 character)

What is my fault. Im very new at Unity.

Here is the Levels Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Level //A single level
{
public List<string> m_Rows = new List<string>();

public int Height { get { return m_Rows.Count; } }
public int Width
{
get
{
int maxLenght = 0;
foreach (var r in m_Rows)
{
if (r.Length > maxLenght) maxLenght = r.Length;
}
return maxLenght;
}
}
}

public class Levels : MonoBehaviour {
public string filename;
public List<Level> m_Levels;

public void Start()
{
TextAsset textAsset = (TextAsset)Resources.Load(filename);
if (!textAsset)
{
Debug.Log("Levels: " + filename + ".txt does not exist!");
return;
}
else
{
Debug.Log("Levels imported ");
}
string completeText = textAsset.text;
string[ ] lines;
lines = completeText.Split(new string[ ] { "\n" }, System.StringSplitOptions.None);
m_Levels.Add(new Level());
for (long i = 0; i < lines.LongLength; i++)
{
string line = lines*;*
*if (line.StartsWith(";"))*
*{*
*Debug.Log("New level added");*
*m_Levels.Add(new Level());*
*continue;*
*}*
*m_Levels[m_Levels.Count - 1].m_Rows.Add(line); //Always adding level rows to last level in list of levels*
*}*
*}*
*}*
*```*
*And here is the Level Builder Script:*
*```*
*using System.Collections;*
*using System.Collections.Generic;*
*using UnityEngine;*
*[System.Serializable]*
*public class LevelElement //defines each item in a level by mapping a single char to prefab*
*{*
*public string m_Character;*
*public GameObject m_Prefab;*
*}*
*public class LevelBuilder : MonoBehaviour {*
*public int m_CurrentLevel;*
*public List<LevelElement> m_LevelElements;*
*public Level m_Level;*
*GameObject GetPrefab(char c)*
*{*
*LevelElement levelElement = m_LevelElements.Find(le => le.m_Character == c.ToString());*
*if (levelElement != null)*
*return levelElement.m_Prefab;*
*else*
*return null;*
*}*

*public void NextLevel()*
*{*
*m_CurrentLevel++;*
*if (m_CurrentLevel >= GetComponent<Levels>().m_Levels.Count)*
*{*
*m_CurrentLevel = 0; //Wrap Back to first Level*
*}*
*}*
*public void Build()*
*{*
*m_Level = GetComponent<Levels>().m_Levels[m_CurrentLevel];*
*//Offset coordinates so that centre of level is roughly at 0,0*
*int startx = -m_Level.Width / 2; //Save start x since needs ti be reset in loop*
*int x = startx;*
*int y = -m_Level.Height / 2;*
*foreach (var row in m_Level.m_Rows)*
*{*
*foreach (var ch in row)*
*{*
*Debug.Log(ch);*
*GameObject prefab = GetPrefab(ch);*
*if (prefab)*
*{*
*Debug.Log(prefab.name);*
*Instantiate(prefab, new Vector3(x, y, 0), Quaternion.identity);*
*}*
*x++;*
*}*
*y++;*
*x = startx;*
*}*
*}*
*}*
*```*
*Unity shows NO Error, Visual Studio shows NO Error. If i load only 9 Levels from a 1241 character file it works. But if it is more: NO CHANCE*
*Could somebody help please?*

I would suggest you add some more Debug.Log() commands to output the length of the complete text, the number of lines, and maybe one or two complete lines so you can verify you’re getting the data that you think you’re getting. If anything is different than you expect, that could help narrow down your problem.

One possible issue I notice is that you’re splitting the string on “\n”. Due to varying ways of representing line breaks, this is not necessarily a reliable way of dividing the text into its separate lines (depending on how the text file was created/edited). You might try modifying your Split() call to cover a few more options for safety; e.g.

Split(new[ ] {"\r\n", "\r", "\n"}, StringSplitOptions.None)

The split option from you changes nothing, sorry!

If i load the working file there will be loaded 93 Lines. If i load the not working file (which is the same with more levels) it only loads 1 line. but there are 586 lines inside.

So i tried to figure out at which point it no longer loads. Ant that point is, if i do 18 more lines in the file (1 Level).

So the Split() function returns an array containing only one element? What’s inside that one element?

First of all, check whether the contents are shown correctly when you click on the text asset, if it shows no content at all, you’ll also get an empty string when you query the text asset’s text property. That’s likely the reason why you only get one line.

If you happen to encounter this problem, you’ve most-likely added characters that are not compatible with the encoding that was used to save the file. In order to solve the problem, you can either remove the characters that are not contained in the respective character set, or change the encoding when you save the file.

For example, newly created (plain) text-files on windows are often saved using ANSI encoding format by default.

Instead of searching for characters that may cause the problems, I’d simply switch the encoding to UTF-8. On windows, the built-in text editors usually allow to specify the encoding somewhere near the input field for the file name and/or the save buttons. Sometimes, the file extension dropdown also contains an extensions multiple times with additional information about the encoding that’ll be used.

UTF-8 should be able to cover the characters that you may need, and Unity should then be able to read the contents correctly.

Secondly, there are also some trivial things in your code that are redundant and/or can be improved in order to shorten the code and make it more readable.

Last but not least, please use the correct code-tags when you post code blocks, not the inline-code-tags.

UTF-8 is working. Thanks.

1 Like