- Home /
Creating a Game Object from a text file
Hi there,
Last night I asked about saving object information to a text file, well I have managed to finish that, and loading the information back in too.
Now I am trying to figure out how to use the saved information to create the game objects.
Here's an example of the information I am loading in:
a, b, c, (d)
Where a = the type of object, b = position.x, c = position.z, d = rotation
Example of saved/loaded data:
1,-5,-10,(270.0, 0.0, 0.0)
1,0,10,(270.0, 270.0, 0.0)
2,10,-10,(270.0, 90.0, 0.0)
4,0,0,(270.0, 270.0, 0.0)
3,10,10,(270.0, 0.0, 0.0)
What would be the best way to instantiate an object at those co-ords/rotation for each line?
I appreciate any help :)
Hi @Skibur-2, I'm also trying to load and save data of the objects. Can you please provide your script or steps to create the same? Thank you for your help.
Thanks, Raj
Answer by Skibur-2 · Sep 06, 2011 at 05:03 AM
I solved it...
I just created a GameObject variable for each prefab and then assigned the prefab to that variable.
Works like a charm :D
I did want to do it an easier way, but this way works.
Answer by sven1994 · Sep 05, 2011 at 06:00 PM
I don't know what you mean with "the type of object", but I think the easiest way would be to have a prefab for each "type of object" and instantiate it. I would do it like this
Foreach(line in linesOfFile)
{
Split the line by commas to get the individual parameters
Remove spaces and brackets
Instantiate prefab at specific position and rotation
}
In order to achieve a learning effect I don't write you a working function. You can use google for that ;)
Yeah, I will handle the object type part, that's why I only asked for help with the pos/rotation.
As for splitting by comma and removing commas and brackets, I'm going to have to do some research...
Thanks for the help, and for forcing me into studying :P
Answer by Skibur-2 · Sep 06, 2011 at 03:35 AM
Here's part of what I ended up using...
At the moment, when I click load everything works up to the instantiate part, when it hits an error (unable to read file) and also says that the prefab I want to instantiate is null, even if I put in "GameObject.whatever" into the function.
If I remove the instantiate part it will load the data for each line perfectly, but something about instantiate is making it just stop...
Help?
function OnMouseUp () {
var array : GameObject[] = GameObject.FindGameObjectsWithTag("Track");
var item : GameObject;
for (item in array){
Destroy(item);
}
try {
sr = new StreamReader("SavedLevel.txt");
line = sr.ReadLine();
while (line != null) {
var separator = ",";
var theLine = line.Split(separator[0]);
print(line);
posX = (parseInt(theLine[1]));
posZ = (parseInt(theLine[2]));
pieceRotationX = (parseInt(theLine[3]));
pieceRotationY = (parseInt(theLine[4]));
pieceRotationZ = (parseInt(theLine[5]));
print(theLine[0]);
if ((parseInt(theLine[0]) == 1)) {
savedTrackPiece = GameObject.trackPerpPrefab;
}
if ((parseInt(theLine[0]) == 2)) {
savedTrackPiece = GameObject.trackHorizPrefab;
}
if ((parseInt(theLine[0]) == 3)) {
savedTrackPiece = GameObject.trackCurvePrefab;
}
if ((parseInt(theLine[0]) == 4)) {
savedTrackPiece = GameObject.trackJunctionPrefab;
}
var track = Instantiate(savedTrackPiece, Vector3(posX, 0, posZ), Quaternion.Euler(pieceRotationX, pieceRotationY, pieceRotationZ));
track.name = ("TrackPiece" + currentPiece);
trackName = track.name;
currentPiece ++;
line = sr.ReadLine();
}
sr.Close();
}
catch (e) {
print("The file could not be read:");
print(e.Message);
}
}
Answer by sven1994 · Sep 06, 2011 at 03:42 AM
Thats because trackPerpPrefab is not a member of the GameObject class.
Hmm, well how could I assign prefabs to those variables?
Here's what I'm trying, but it's still not working...
It's returning the correct savedTrackPiece, but the instantiate is still the problem...
if ((parseInt(theLine[0]) == 4)) {
savedTrackPiece = "trackJunctionPrefab";
}
var track = GameObject.Instantiate(Resources.Load("Level Editor/Assets/Prefabs/" + savedTrackPiece)...
Am I going down the right track?
This should work for instantiating it:
var obj = Instantiate(savedObj, Vector3(posX, 0, posZ), Quaternion.Euler(objRotationX, objRotationY, objRotationZ));
Just replace my vars with yours and make sure you're setting the GameObject value of the prefab you're trying to spawn.
P.S. If you get this working let me know. Having some issues with the running multiple passes with different lines.