I have a simple unlit dissolve shader here and i have a float value called _Level that controls the Dissolution level for the dissolve. It works great in editor but i also want to be able to control it in script using DoTween DoFloat() which doesn’t work. Not only that but I’m not even able to get unity engine’s material.SetFloat() to work either for some reason.
Here are the parts of the code that are important for this:
You’re using the global property setting instead of local. You would have to use dissolveMaterial.SetFloat("_Dissolution", .5f); so that you’re setting the _Dissolation value on that instance of the material.
If you want to be able to have it be a global value shared by all materials with this shader, and thus be able to use SetGlobalFloat, then you’d want to remove it from the Properties{} section of your shader.
I don’t really want it to be global and it would be nice to keep the property to have the freedom to change it in inspector if need be. So mat.SetFloat() sounds ideal which was actually the first thing I tried.
Could we see more of your code that’s executing during this? Because it should work fine if the only thing you’re doing is assigning the material and doing thatMaterial.SetFloat().
weeird… i moved var rend = GetComponent().material = dissolveMat; to the method the materials were being swapped in rather than keeping it in Start() and now it works. i wanted to save a reference to the renderer so i could swap out materials so i put the GetComponent() in Start to initialise the reference that i would use later at run time but it wasnt working then. how odd…
Doing renderer.material = myMaterial; makes a copy of myMaterial and assigns it to the renderer component every time you call that code. The material on the object and myMaterial are now separate and completely unconnected materials. If you want to assign a material to a renderer component and have modifications to the material you assign continue to show on the object, use: renderer.sharedMaterial = myMaterial;
ahhh ok let me try that! i actually did get it working but yes you are correct, it animates the shader globally… which isn’t optimal but i figured i would live with it for now. your approach sounds much better.
Thanks as always bgolus!
Apologies, I know this thread is quite old, but based on my tests in Unity 2021.3, it seems that this information may no longer hold true. Assigning a material to a renderer does not create a copy of that material. Instead, the renderer directly uses the assigned material, and any modifications made to that material later will be also applied to the renderer. It appears that Unity has undergone some changes, or perhaps there is something I am overlooking.