- Home /
can't assign material to trail render added by script
Hello, as the title says, in my code i'm generating a number of prefabs, and adding them the trail renderer component, and setting all of it's parameters like startwidth and so on, is ok, but for some reason, i can't apply a material to the trail renderer itself. so the trail keeps on rendering with the "missing shader" magenta color.
I tried this way
var trailcomponent = newplanet.GetComponent("TrailRenderer") as TrailRenderer;
trailcomponent.renderer.material = dummy_mat;
//or...
trailcomponent.renderer.materials[0] = dummy_mat;
none worked. If i hadn't randomly generated content, i could make a "trail prefab" and apply it, but it's not my case. I need it to be added, and tweaked by code for each object.
thank you for your time :)
even stranger:
print(trailcomponent.renderer.material.name);
correctly prints the material name, but the behaviour is still the same, plus, in the inspector, the trail render doesn't show any material assigned
Not too sure why the
trailcomponent.renderer.material = dummy_mat;
doesnt work....But a bug that has always frustrated me is setting a particular material in the array wont work. You need to set the array to a new array with the changed material like so:
//Wont do anything
trailcomponent.renderer.materials[0] = dummy_mat;
trailcomponent.renderer.materials[1] = dummy_mat;
//This works
$$anonymous$$aterial[] new$$anonymous$$aterialArray = new $$anonymous$$aterial[2];
new$$anonymous$$aterialArray[0] = dummy_mat;
new$$anonymous$$aterialArray[1] = dummy_mat;
trailcomponent.renderer.materials = new$$anonymous$$aterialArray;
Thanks for your time, i'm trying it as soon as i commit this comment :) not sure why in your example you're using 2 materials, but maybe it's just an example :)
still doesn't work, I'm starting to think there is something wrong in the way I store the TrailRenderer component. Could you please provide your way of setting the trail render color ?
both declarations :
var trail : TrailRenderer = object.AddComponent(TrailRenderer);
var trailcomponent = object.GetComponent("TrailRenderer") as TrailRenderer;
...
var new$$anonymous$$aterialArray:$$anonymous$$aterial[] = new $$anonymous$$aterial[2];
new$$anonymous$$aterialArray[0] = temp_material;
trailcomponent.renderer.materials = new$$anonymous$$aterialArray;
trail.renderer.materials = new$$anonymous$$aterialArray;
when then applying the newly made array of material(s) both to trail and trailcomponent, it only affects the gameobject itself. I must be doing the TrailRenderer declaration wrong
I think you are changing the material of the sphere with that code. try this:
var object= new GameObject.CreatePrimitive(PrimitiveType.Sphere);
var new_trail_renderer : TrailRenderer = object.AddComponent(TrailRenderer);
var temp_material = new $$anonymous$$aterial(Shader.Find("Particles/Alpha Blended Premultiply"));
new_trail_renderer.material = temp_material;
Answer by GnomenxD · Jul 27, 2017 at 07:56 PM
I was having the same issue and by searching for it on google I found this answer post. Since there were no answer I tried once more to fix the problem. And the Editor shows up with the error message
Not allowed to access Renderer.material on prefab object. Use Renderer.sharedMaterial instead
So just use the trailcomponent.sharedMaterial instead or trialcomponent.sharedMaterials (for the array) and you can assign and change the materials as you like. Hope this helps anyone who mysteriously come across this question.
Regards.
Your answer
Follow this Question
Related Questions
Trail after game object with pause/clear function 2 Answers
TrailRenderer sorting layer problem. 2 Answers
Creating an infinite trail that can be collided with 2 Answers
Is possible to save a trail rendered to load again after? 1 Answer
How to get Zerg Tentacle-like effect (see example in post)? 1 Answer