x


How do I get a material from a GameObject?

I have a bunch of cubes that I instantiate with different materials using an ID system. The array of materials is set up like so:

var materialList : Material[];
var mat0 : Material;
var mat1 : Material;
var mat2 : Material;
var mat3 : Material;

function LoadMaterials() {
    materialList = new Material[3];
    materialList[0] = mat0;
    materialList[1] = mat1;
    materialList[2] = mat2;
}

When I create the cubes I do so like this:

var cube = Instantiate(smallCube, Vector3 (x, z, y), Quaternion.identity);
cube.renderer.material = materialList[textureId];

Now with another function I am iterating through all of the cubes to get the x, y, and z positions, but I want to also get the texture. If I can figure out the texture from just the gameobject then I want to do that so I don't have to store that information separately. If I do have to store the information some how can i store it with the gameobject itself? Here is an example to show what i mean:

cubes = GameObject.FindGameObjectsWithTag("cube");
for(var cube : GameObject in cubes) {
    cubeX = cube.transform.position.x - lowX;
    cubeY = cube.transform.position.y - lowY;
    cubeZ = cube.transform.position.z - lowZ;

    textureId = ?????;
}

If there isnt some function that lets me get the material name to a string from the gameobject then can I do something like cube.MyVar? Thanks for any help!

more ▼

asked Jan 22 '11 at 05:09 PM

user-8533 (google) gravatar image

user-8533 (google)
17 1 1 3

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

1 answer: sort newest

You can replace this:

var materialList : Material[];
var mat0 : Material;
var mat1 : Material;
// etc, etc

with this:

var materialList : Material[];

You don't need the rest of that; the point of arrays is that you don't use individual variables.

As for the texture, probably the easiest way is to store the texture number in a component in the prefab. The component can just be a script called "ID" that says

var textureId : int;

When instantiating, you can do

cube.GetComponent(ID).textureId = textureId;

Then to get the ID later, you can do

textureID = cube.GetComponent(ID).textureId;
more ▼

answered Jan 22 '11 at 06:00 PM

Eric5h5 gravatar image

Eric5h5
80.1k 41 132 519

But without

var mat0 : Material;
//etc

I cant drag references to the materials, right?

Jan 23 '11 at 12:51 AM user-8533 (google)

Of course you can; just add as many array entries as you need.

Jan 23 '11 at 02:03 AM Eric5h5

Ah thanks! I see where i was going wrong, have to set the size and pull down the array on the inspector.

Jan 23 '11 at 02:30 AM user-8533 (google)
(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:

x811
x388
x110

asked: Jan 22 '11 at 05:09 PM

Seen: 1639 times

Last Updated: Jan 22 '11 at 05:09 PM