I accidentally used shared material and can't revert it

Umm so unfortunately I accidentally used shared material to set the colour of a line renderer

LR.sharedMaterial.SetColor("_Color", Color.red);

not knowing it changed it for all the objects it owns, and I dont know if/how I can revert it and change it to the default one?

Simply restarting Unity should do the trick I believe.

Source control? :slight_smile:

You wanna instance materials before you tinker with them on an individual basis. Keep in mind each material does add a drawcall so try not to go nuts, or else use color-keyed material pooling if you really need a ton of custom materials yet only a few unique colors (like for teams).

Also, are you aware that LR has color properties you can use? It’s just multiplicative AFAIK, like SpriteRenderers, but aware of linear position on line.

That did it, thanks!!

Yes never gonna try something I dont know again lol :smile:

I’m a bit unsure about changing LR colors using it’s properties but I will have a look
Also not related to this but I was wondering if it’s possible to change the shape of the line renderer in unity 3d, currently mines just a rectangle which I can change the length and the width, I’m wondering if I can make that into a triangle? Something like this

Thanks!

8498105--1131335--upload_2022-10-8_19-46-0.png
Like this, not sure why I couldn’t upload it in the previous post

Of course you can! Under the width property of the line renderer component is a graph. Double click anywhere on the red line in the graph to insert a new key. Drag the first key down to x=0, y=0, drag the second key you just inserted to x=1, y=1. Now you have a triangle. You may also want to right click both keys and set both tangents to “linear”.

In the picture you showed, the end of the cone is slightly rounded. You can sort of emulate this by increasing the “end cap vertices” property of the line renderer, but that may be too round for your liking. Another way you can achieve this effect is to have a plane object, and give it a transparent material with the cone texture.

1 Like

Your a legend, was just asking for my curiosity lol, its amazing what it can do, for the rounded port on the cone, I’ll probably just add the cone texture as you said, but I’d like to go back to my original topic, I’m struggling to change the color of the line renderer.
What I’m trying to do is when the player is out of ammo, the color of the line renderer is gonna be red but I’m struggling to change this using code, what can I do?

Not a problem. You will want to use the https://docs.unity3d.com/ScriptReference/Renderer-material.html property that line renderer has. This retrieves a unique instance of the material. You will want to keep a reference to this material in your script. Now you can change this material however you like, and any change will only be reflected in this particular line renderer and no others. Remember to call Destroy() on your material reference within the OnDestroy callback too.

Actual legend!!
I’m doing this right now

            if(ammoSlider.value == 1)
            {
                Shoot = true;
                LR.material.color = Color.white;
            }

            if (Shoot == false)
            {
                LR.material.color = Color.red;
            }

And it works perfectly fine, I’m ocnfused on what’s going on in the OnDestroy callback because I’m not calling it at all as of now, why would I need to destroy the material instance if I can change it when needed?

Let me explain. LineRenderer.material, each time you use this property, unity produces a clone of the material, assigns this unique material to the line renderer, and returns it back to you. This means two things:

  1. You don’t want to call this property more than once in your script. Save the material in a variable within the Start method, and use the variable from then on.
  2. Unity no longer manages the disposal of this material for you. So not calling Destroy() within the OnDestroy callback can leak memory over time.

Hope that explains things!