What it does:
The script below reads the first line from the specified text file and instantiates a prefab based on information in said file.
My issue:
I’m not sure how to make it so that it continues parsing the rest of the lines, spawning multiple objects accordingly.
Additionally:
Obviously, the variable savedObj will have to change during each pass to correspond to the information given. I’m currently getting errors thrown saying the variable is already specified. Not sure how to set up the variable so it’s editable based on the condition.
I’m having issues not only setting the name of the object being instantiated, but allowing it to increment as well.
Any nudge in the right direction would be more than appreciated. Thank you.
#pragma strict
#pragma implicit
#pragma downcast
import System.IO;
// Spawnable Object Vars
var Cube:GameObject;
var Circle:GameObject;
// Data Vars
var dataFile:TextAsset;
var sr = new StreamReader("Assets/ObjectEditor.txt");
var line = sr.ReadLine();
var separator = ",";
function Start(){
//for (line in linesOfFile){
var dataArray = line.Split(separator[0]);
var posX = (parseFloat(dataArray[1]));
var posZ = (parseFloat(dataArray[2]));
var objRotationX = (parseFloat(dataArray[3]));
var objRotationY = (parseFloat(dataArray[4]));
var objRotationZ = (parseFloat(dataArray[5]));
if ((dataArray[0] == "Cube")) {
var savedObj = Cube;
}
//if ((dataArray[0] == "Circle")) {
// var savedObj = Circle;
//}
// Spawn Object
var obj = Instantiate(savedObj, Vector3(posX, 0, posZ), Quaternion.Euler(objRotationX, objRotationY, objRotationZ));
//var objCount++;
//var obj.name = ("Object" + objCount);
var objName = obj.name;
//}
}
Well since your reading in multiple lines your going to have to use a for loop or while loop… Otherwise your only going to read the initial first position/line.
You can easily do this with the EoF(End of File) commands… Just google them you will find what your looking for, there are different types of EoF’s depending on which reader your using and how your reading( line, character, etc)
Next thing. Your variable saveObj cannot be used outside of that if statement… Simple fix there. at the start of your for loop or while loop just put var savedObj = null;
while(myFile != EOF) //Whatever that might be
{
var savedObj = null;
//read the line/char/whatever and do what you were doing.
if ((dataArray[0] == "Cube")) {
savedObj = Cube;
}
}
Thanks a lot for the help. But I’m not sure sure how to use EOF or ReadAllText commands in JS, I’m assuming there’s an import for it? I have System.IO imported but it’s giving me member errors for both.
If I could get this to work:
var fileText = File.ReadAllText(filePath);
var lines = fileText.Split("\n"[0]);
Looks like it would solve most of my issues. From the looks of it, the script splits after a line break and stores the entire line in an array. From there, I can probably use another split to separate that line via commas. The logic is sound, just need to get my file to read those commands.
That was the issue. I’m using 3.5 BUT the project was made in an earlier version. Script works fine now. Thanks a ton. I’ll just move my stuff over to a fresh project.
It’s not; JS uses both single and double quotes for strings, and string indices for chars. fileText.Split(“\n”[0]) is correct. Also that “new char{}” code wouldn’t compile in C#; just fileText.Split(‘\n’) is fine.