- Home /
GameObjects Loading Without Instantiation
I've been working on a practice tower defense project and I've run into an issue with gameobjects loading without instantiation. My script doesn't call any instantiation for the gameobjects (tiles), yet they still load into the scene. I've spent quite a few hours commenting out anything that even has a remote chance of causing it, but I've come up short. Hopefully someone can shed some light.
// Gathers level information from .txt file and parses it.
public void ParseLevel()
{
// The level(s) to load.
string pathName = "Assets/Levels/Level1.txt";
// Ensure the level file exists.
if (File.Exists(pathName))
{
string line = "";
string levelTheme = "";
var fileLines = File.ReadAllLines(pathName);
bool themeSaved = false;
// Getting the x/y lengths for the tile array.
var levelBounds = GetLevelSizeBounds(fileLines);
// Resizing the tiles array to fit all expected tiles.
var tempTiles = new GameObject[levelBounds.xBounds, levelBounds.yBounds];
_xMaxPos = levelBounds.xBounds - 1;
_yMaxPos = levelBounds.yBounds - 1;
int lineCounter = -1;
// So long as there are characters on the file.
for (int i = 0; i < fileLines.Length; i++)
{
// Store the current line of the file.
line = fileLines[i];
// Save theme name if first line and hasn't been saved.
if (!themeSaved && i == 0)
{
levelTheme = line;
}
else
{
// Go through each letter in the line.
for (int j = 0; j < line.Length; j++)
{
// Assigns a new generated tile to the tiles array.
tempTiles[j, lineCounter] = GenerateTile(line[j]);
// Assigns each tile a position on the map.
tempTiles[j, lineCounter].transform.position = AssignTilePosition();
}
}
lineCounter++;
}
}
else
{
Debug.Log("File not found");
}
}
I know my code probably isn't the best, but it gets the job done the way I want. GenerateTile() just returns a new GameObject that is determined by the character, and AssignPosition does what it says. Other than the classes, I have the LevelLoader class shown here attached to a single Empty Object in the scene. Please let me know if any more information is needed.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
error CS8025: Parsing error in C# code 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Deserialize nested Json data 1 Answer