I’m trying to make some sort of level builder that will read the level file and then build the level by instantiating prefabs.
Whatever, I’ve run into a few problems ![]()
My current script:
using UnityEngine;
using System;
using System.Collections;
using System.IO;
public class LevelBuilder : MonoBehaviour {
public Transform Platform;
public Transform PlatformTurn;
public Transform Rod;
public Transform Ball;
public Transform Crate;
//And about 30 more
void Start () {
try {
using (StreamReader levelReader = new StreamReader("Levels/TestLevel.txt")) {
string obj;
string pos;
string rot;
while (levelReader.Peek() == 1) {
obj = levelReader.ReadLine();
pos = levelReader.ReadLine(); //How do I convert this 3 word string to a vector3?
rot = levelReader.ReadLine(); //How do I convert this 4 word string to a quaternion?
if(obj == "Platform")
Instantiate(Platform,pos,rot);
else if(obj == "PlatformTurn")
Instantiate(PlatformTurn,pos,rot);
else if(obj == "Rod")
Instantiate(Rod,pos,rot);
else if(obj == "Ball")
Instantiate(Ball,pos,rot);
else if(obj == "Crate")
Instantiate(Crate,pos,rot);
//And about 30 more
else
Debug.Log("The object could not be created");
}
}
}
catch (Exception fail) {
Debug.Log("There was a problem reading the level file.");
Debug.Log(fail.Message);
}
}
}
Can someone help me with these problems?
How do I convert the 3 word string to a vector3?
How do I convert the 4 word string to a quaternion?
Is there some way to get a variable by it’s name? The wall of if() is quite annoying.
I tried to use an Enumerator but I couldn’t find out how to link it to the variables