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 Fright · Sep 29, 2014 at 11:33 PM · materialcomponentadd

Add material component to object script-wise

Hey! I'm trying to add a material componenet to an object via script. I guess this should be done as GameObject.AddComponent (or do I guess wrong?), but I can't seem to find the way to do it. I've googled around with no success.

I know I can change the color by using GameObject.renderer.material.color = color, but that's not what I need.

This is the Project folder (Materials/BoxMaterial is the material that I want to add): http://i.imgur.com/lxjK4jg.png

If it was a static object created through Unity, I'd just drag it to the object in Hierarchy. But the problem is, the objects are generated upon mouse click.

I'd appreciate any help! Thanks!

Comment
Add comment · Show 1
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 Fright · Sep 30, 2014 at 10:59 AM 0
Share

Now I got it. Thanks everyone! $$anonymous$$uch appreciated!

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Sep 29, 2014 at 11:37 PM

Not sure what your setup is. A Material is not a component. Assuming the object currently has a material:

 public var mat : Material;  // Initialize through drag and drop in Inspector
 
 function Start() {
     renderer.material = mat;
 }

If you are starting from scratch, then you'll have to add a MeshRenderer to the object before you can assign the material.

If you don't want to initialize the reference to the material through drag and drop, you can put the material in the Assets/Resources folder and use Resources.Load() to pull it in.

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 Fright · Sep 29, 2014 at 11:48 PM 0
Share

"Assu$$anonymous$$g the object currently has a material"

Well that's the problem, I want to assign a material to it. How can I do it in the script?

Thanks so much!

avatar image robertbu · Sep 30, 2014 at 02:29 AM 0
Share

Does your object have a Renderer on it? If so, it has a material (probably the default material). If your object does not have a renderer on it, then you would do something like:

 var rend : $$anonymous$$eshRenderer = gameObject.AddComponent($$anonymous$$eshRenderer);
 rend.material = mat;

Again, this assumes you've initialized the 'mat' variable through drag and drop.

avatar image
0

Answer by raja-bala · Sep 30, 2014 at 12:21 AM

To assign a material, you don't add it as a component. You set the material of the *renderer component. The renderer can be a mesh renderer, text renderer, etc..

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 BenKurdziel · Sep 30, 2014 at 01:45 AM

What the others have said is pretty much spot on.

The material of an object is part of an object's "MeshRenderer" component. If you want to assign a material to that MeshRenderer, you can find the component on the object by referencing that object and using:

C#

 public Material myMaterial;
 
 myGameObject.GetComponent<MeshRenderer>().material = myMaterial;


With this method, you'll need to drag and drop the material into the inspector for whatever script has this code.

Otherwise you'd need to use:

Material myMaterial = (Material)Resources.Load("");

With this method, you have to have the Material in a "Resources" folder in your Project hierarchy.

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 hanystylz · Mar 14, 2017 at 05:30 PM

Heya good people... I have imported an FBX file from blender and I am currently working on the interaction part. I want to enable my house model to be able to have a material change, colour and the objects should be moved from one place to another. Now i have written all the scripts but to some starnge reasons i am not able to see the interactions, at least most of it.

My questions are, are there settings that i need to adjust after importing the FBX file.??

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can't add script (Doesn't exist?) 2 Answers

Access to variables from other script objects 3 Answers

A node in a childnode? 1 Answer

Inspector vs Script: Component best practice? 1 Answer

how to kill an enemy 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