x


Creating an Object with a string C#! How?

Okay, so what I want to do is create a Tile everywhere there is an "X". so what I think i would do is:

var SectionOne:   "XXXXXXXXXXXXXXXXXXX";
var SectionTwo:   "XOOOOOOOOOOOOOOOXXX";
var SectionThree: "XOXXXXXXXXXXXXXOXXX";
var SectionFour:  "XXXXXXXXXXXXXXXOXXX";
var SectionFive:  "XOOOOOOOOOOOOOOOXXX";
var SectionSix:   "XXXXXXXXXXXXXXXXXXX";

So I would instantiate a gameobject for every X but for "O" I would instantiate a wall... How do you do this?

more ▼

asked Jan 08 '12 at 11:02 AM

Wikened gravatar image

Wikened
56 9 10 11

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

1 answer: sort voted first

How about this- instead of using a string like that, why don't you use an array of booleans? (actually integers, since they're smaller...)

int[,] tiles = new int[,]{
    {0,1,1,1,1},
    {0,1,1,1,1},
    {0,1,1,1,1}
};

Then, to instantiate objects over a space, do something like this-

for(int i; i < tiles.GetLength(0); ++i)
{
    for(int j; j < tiles.GetLength(1); ++j)
    {
        if(tileSegments[i,j] != 0)
        {
            Instantiate(obj, new Vector3(i * tileWidth, 0, j * tileHeight), Quaternion.identity);
        }
    }
}

Please note that you can't assign these in the editor, or keep them serialised along with your other properties. You either have to hardcode them, or use a special custom editor.

more ▼

answered Jan 08 '12 at 11:13 AM

syclamoth gravatar image

syclamoth
15k 7 15 80

(forgive incorrect syntax, I'm not fantastic at javascript. If someone who knows more than me can find any problems, please tell me!)

Jan 08 '12 at 11:13 AM syclamoth

If you know C# Can you write that :) I'm trying to learn C# :)

Jan 08 '12 at 11:15 AM Wikened

Well, C# is basically like what I put there, but with some details changed. I'll change my answer to correct C#.

Jan 08 '12 at 11:44 AM syclamoth

It didn't work :/ I keep getting lots of errors. Could you maybe try to fix it in an editor? You would help me soooo much! :D

Jan 08 '12 at 02:10 PM Wikened

Well, obviously that wasn't intended as completed code! It's really just a guide for what you need to do. What errors are you getting? You need to define a public GameObject 'obj' to use as the instantiated tile prefab.

Jan 08 '12 at 03:14 PM syclamoth
(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:

x4374
x2176
x1726
x430
x276

asked: Jan 08 '12 at 11:02 AM

Seen: 1279 times

Last Updated: Jan 08 '12 at 10:17 PM