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?*