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 :slight_smile:

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 :wink:

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.

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);
    }
}

Thats because trackPerpPrefab is not a member of the GameObject class.