Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by user-8533 (google) · Jan 22, 2011 at 05:09 PM · materialtexturesgameobjects

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Eric5h5 · Jan 22, 2011 at 06:00 PM

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;
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image user-8533 (google) · Jan 23, 2011 at 12:51 AM 0
Share

But without

 var mat0 : $$anonymous$$aterial;
 //etc

I cant drag references to the materials, right?

avatar image Eric5h5 · Jan 23, 2011 at 02:03 AM 0
Share

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

avatar image user-8533 (google) · Jan 23, 2011 at 02:30 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

textur max resolution 1 Answer

Material doesn't have a color property '_Color' 4 Answers

materials deplaced, all billion objects purple. How put back ? 1 Answer

Using one material for multiple textures 1 Answer

3d model editing and multiple textures per material 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges