- Home /
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!
Now I got it. Thanks everyone! $$anonymous$$uch appreciated!
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.
"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!
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.
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..
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.
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.??
Your answer
Follow this Question
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