x


Using TextAsset to create a grid.

Hello, I need to be pointed in the right direction with Unity's TextAsset component.

I will have a .txt file with 400 numbers, without spaces. ex: "010100111000101010" I want to have unity read each indivdual character, if it detects a "0" it will move up a value(position) and if it detects a "1", it will add a prefab (in this case a cube) before moving up a value. The end result will be a 20x20 grid of blocks in pre-determined positions.

I want to integrate it into this script somehow:

var cube : Transform;
function Start () {
    for (var y = 0; y < 5; y++) {
        for (var x = 0; x < 5; x++) {
            var cube = Instantiate(cube, Vector3 (x, y, 0), Quaternion.identity);
        }
    }
}

Can anyone help me out with this?

more ▼

asked Oct 26 '10 at 03:19 PM

Kyle 6 gravatar image

Kyle 6
41 8 8 12

(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

Place your text file into Unity's assets folder. Then make the script changes below, and drag a reference to the text asset into the "dataFile" variable below.

var dataFile : TextAsset;
var cube : Transform;

function Start () {
    for (var y = 0; y < 5; y++) {
        for (var x = 0; x < 5; x++) {
            var charNum = (y*5)+x;
            if (dataFile.text[charNum] == "1") {
                var cube = Instantiate(cube, Vector3 (x, y, 0), Quaternion.identity);
            }
        }
    }
}

That should do it! (untested though... give me a shout in the comments if it doesn't work!)

more ▼

answered Oct 26 '10 at 04:01 PM

duck gravatar image

duck ♦♦
41.4k 95 152 415

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1285
x1080
x274
x45
x33

asked: Oct 26 '10 at 03:19 PM

Seen: 1510 times

Last Updated: Oct 26 '10 at 03:19 PM