• 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
1
Question by l0die · Jun 16, 2010 at 12:11 PM · materialruntimecross-object

Assign materials at runtime, that aren't in the objects materiallist

Hi,

I'm trying to build a configurator in which you can choose and place some parts of a piece of furniture (say a couch) to build it according to your own taste.

When a user selects a type of material in the webbrowser I call a script in Unity to change the material using:

renderer.sharedMaterial = materials[index];

where materials is the (in the editor) filled array of materials on that object.

The problem with this approach is that I have to add all materials to every piece of the furniture in the editor (in the same order) before being able to assign them at runtime. This will be alot of work when the amount of materials and objects stack up.

I tried something like:

obj1 = GameObject.Find("ObjectName1");
obj1.renderer.sharedMaterial = materials[index];
obj2 = GameObject.Find("ObjectName2");
obj2.renderer.sharedMaterial = materials[index];

etc...

to assign materials to all needed objects from 1 object that has all materials in its material array, but this doesn't seem to work.

Is there a way to add all materials i want to use in runtime in the project and assign them to any object by name? Something like:

GameObject.Find("GameObject").renderer.material = Material.Find('Fabric1');

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Extrakun · Jun 16, 2010 at 01:20 PM

You are on the right track with

GameObject.Find("GameObject").renderer.material = newMaterial;

There are 3 ways to obtain the texture you want; the first is to use Resources.Load to get the texture. However, this is not recommended for web players and results in a performance hit.

The other method is to pre-attach the textures you want to a script's array, and attach that object, to create a material catalog. Something like this

public var textures_ : Texture2D[];

public var changeTexture(target:GameObject, index:int) { target.renderer.material = textures_[index]; }

When you attach this script to a game object, you will be able to drag and drop textures into the array. To use it, just invoke the changeTexture function of that gameobject and pass in the object which material is to be changed.

If you wish to use strings, you have to change the array to a Hashtable, and manually add in the textures (or just use the Hashtable to store the index).

The third ways is to use the WWW class to read in the texture from the hard disk/url (just put file:// in front of a file path and WWW can read it just fine). If have access to Unity3D pro, you can also use an asset bundle.

Comment
Add comment · 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
0

Answer by LiamKjaer · Nov 10, 2010 at 12:50 AM

I don't think some of the above code works. I'm using Unity3.0 and doing this:

var Textures : Texture2D[];

function SetTexture(iIndex : int) { renderer.material = Textures[iIndex]; }

and it gives the console error - BCE0022: Cannot convert 'UnityEngine.Texture2D' to 'UnityEngine.Material'.

Comment
Add comment · Show 2 · 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 psdev · Jul 05, 2011 at 04:59 AM 1
Share

I'm not an expert in this, but clearly you have a problem with typing there. There is a difference between a Texture and a $$anonymous$$aterial. A texture will reference the image itself, while a $$anonymous$$aterial might have reference to textures and other settings.

avatar image sparrow · May 13, 2012 at 10:20 PM 0
Share

To assign a texture directly to an object you need to assign it to its material like this:

gameObject.renderer.material.mainTexture = Resources.Load("Textures/$$anonymous$$yTexture");

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Change SpriteRenderers Texture2D in runtime 1 Answer

Why doesn't changing the particle's mateiral color during runtime work? 0 Answers

Texture an Object at Runtime, work through help needed. 0 Answers

dynamic images for textures ar runtime 2 Answers

Amplify material editor: adjust property at runtime? 1 Answer


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