x


Applying different textures to spawned objects.

I'm instantiating a grid of cloned objects and need to apply a different texture to each depending on the values in a multidimensional array from a script attached to the main camera. I'm a bit rusty and can't remember how to give each instantiated object the appropriate texture. Can someone please help me out?

 for (var y = 0; y < gridY; y++) {
    for (var x=0;x < gridX; x++) {
      var pos = Vector3 (x + gridOffset, 0, y + gridOffset) * spacing;
      currTile = GS_Games[gameNum, y, x];

    // GS_Pics[currTile] holds the material to be applied
    Instantiate(GS_Tile, pos, Quaternion.identity);
    }
 }
more ▼

asked Jan 15 '11 at 02:08 AM

NewfieJoe gravatar image

NewfieJoe
106 11 12 20

In the end I created the objects off camera in the editor and cloned each one as needed. It's a cludgy way of doing it though.

Jan 16 '11 at 08:44 PM NewfieJoe
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Something on the order of

currTile.renderer.material.mainTexture = TGS_Pics[currTile]; // if TGS_Pics are textures


currTile.renderer.material.mainTexture = TGS_Pics[currTile].mainTexture; // if TGS_Pics are materials and you just want to transfer the texture
more ▼

answered Jan 15 '11 at 02:27 AM

DaveA gravatar image

DaveA
26.8k 153 171 257

Each tile is being rendered with a pictures based on an array. currTile is an integer with the reference, hence GS_pics[currTile] pointing to the material I need to use. I tried instantiated with an array of Gameobjects but something seems to be wrong with the following line when I try that. GS_Tiles[currTile] = Instantiate(GS_Tile, pos, Quaternion.identity);

Jan 15 '11 at 02:59 AM NewfieJoe

Turned out I simply forgot to add " as GameObject" at the end of the command. :P

Feb 19 '11 at 02:19 PM NewfieJoe

Added some clarification to my answer

Feb 19 '11 at 07:46 PM DaveA
(comments are locked)
10|3000 characters needed characters left
more ▼

answered Jan 15 '11 at 02:29 AM

Jessy gravatar image

Jessy
15.7k 72 95 198

This is for multiple objects in a grid that are spawned from script. Minimum of 81 for a small puzzle to a max of 324 for a large one, some using text, some with numbers, some with slices of a picture etc.. I can't figure how to reference them to change the materials after they are instantiated. Brain stalled. :P

Jan 15 '11 at 02:48 AM NewfieJoe
(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:

x3570
x2279
x1726
x1397

asked: Jan 15 '11 at 02:08 AM

Seen: 1308 times

Last Updated: Jan 15 '11 at 02:08 AM