- Home /
The question is answered, right answer was accepted
renderer.material doesnt work
renderer.material doesn't work if you use arrays? how come? and how do you get around it?
my code:
public class treeSettings : MonoBehaviour
{
public Material[] myMaterials;
void Start ()
{
gameObject.GetComponent<Renderer>().material = myMaterials[Random.Range(0,myMaterials.Length)];
}
}
Answer by Ilgiz · May 06, 2015 at 09:41 PM
Everything works fine. I checked out
try this
gameObject.GetComponent<MeshRenderer>().material = myMaterials[Random.Range(0, myMaterials.Length)];
or
check the variable myMaterials in inspector
am I meant to store the material array in the variable my$$anonymous$$aterials of my script, or as an array in the meshRenderer? :) Ive tried both, but it doesn't work... here is a screenshot of what happens:
still doesn't work.... :( What about if I change the texture attached to a material? so using an array of texture, randomly select one and replace the current material... ahh this is annoying... :/ must be doing something seriously wrong...
"When you assign the material by hand on your tree does it render as expected ? " answer to the good question by 0potable
Answer by Rtyper · May 07, 2015 at 03:48 PM
I can see a Mesh Renderer component on your tree GameObject, but no Mesh Filter, so it has nothing to render. I don't know for sure, but I'd bet the correct mesh renderer (if there's only 1 mesh) is on a child GameObject.
Look on the child objects in your prefab. If there's just one with both a Mesh Filter and a Mesh Renderer component on, then put your Tree Settings script on that instead and it should work.
So I just need to add a mesh filter to all of the clones? Like add it to the prefab?
Yes. The meshFilter component store the mesh data and the meshRenderer store the material (how the mesh will render).
I don't know whether you can see my answer below.... I was wrong. You were right, I needed a mesh filter! thanks :) sorted. ahhh, I can breathe now.
Oh and also, I didn't realise, but when I made my prefab, I had a prefab within a prefab.... this meant that both of those prefabs have to have the same materials and components etc. I deleted the children prefabs, and voila! It works..... thanks everyone that helped me... guess it was just me being an idiot.... :D
Answer by 0potable · May 07, 2015 at 09:50 AM
Does your tree have correct UV's ?
It's seems that your shader render just fine on your material (as seen on your inspector).
When you assign the material by hand on your tree does it render as expected ?
Edit : I'm asking this question because your script seems to work just fine.
Maybe your problems is somewhere else :)
yea, the UV's definitely work, since if I add the material normally, it works fine. I made it in blender and imported it, with uv's too, so Im sure that they work.
When I get back home later I will try making a scene from scratch and importing the script, since like you said, Im sure it should work...