Mesh fading without using layers.

So this script is one of four used to create a mesh fade system without using layers. This is the only one I am having an issue with. I get these three errors.

Assets/Editor/MeshFade.cs(121,64): error CS1061: Type float' does not contain a definition for CapMinMax’ and no extension method CapMinMax' of type float’ could be found (are you missing a using directive or an assembly reference?)

Assets/Editor/MeshFade.cs(122,53): error CS1061: Type float' does not contain a definition for CapMin’ and no extension method CapMin' of type float’ could be found (are you missing a using directive or an assembly reference?)

Assets/Editor/MeshFade.cs(179,51): error CS1061: Type float' does not contain a definition for CapMin’ and no extension method CapMin' of type float’ could be found (are you missing a using directive or an assembly reference?)

I am hoping someone might be able to help me figure this out as the original author seems to be MIA. Any assistance on this would be greatly appreciated. If in Unity you create a new project with only the four scripts found on this page http://www.indiedb.c…-layers/#970208 you will at first get somewhere around 83 errors. Nearly all of those errors can be resolved by correcting any instance of && and changing it to read &&. The only three left are related to the CapMinMax and CapMin methods. I think I understand what I am looking at and why it is failing but I just have no idea how to fix it. I’m not sure what needs to be added or changed or if I am just simply missing an entire script.

2645016–186305–MeshFade.cs (6.38 KB)
2645016–186329–Extensions.cs (2.29 KB)
2645016–186330–ManagerMeshFade.cs (1.93 KB)
2645016–186331–MeshFadeCameraController.cs (2.23 KB)
2645016–186332–MeshFadeSync.cs (1.66 KB)

.CapMinMax() and the others are extension methods the writer made. Judging by their names and arguments, I’m guessing they’re just another way of clamping the values to a certain range - you can replace them using Mathf.Max() for CapMin(), and Mathf.Clamp() for CapMinMax().

Looking at the script though, I see ample usage of “renderer.materal”. This creates a copy of the originally applied material, and then assigns the new copy back to the renderer. This causes a build-up of many newly created materials which will not be used anymore the next frame, creating a memory leak (as explained in the Unity documentation for renderer.material here). You need to either clean up the unused materials using Destroy() (and DestroyImmediate() in the editor), or prevent the requirement for duplicate materials in the first place, using PerRendererData or manually creating a single copy per renderer in Start(), and destroying that OnDestroy(). The latter being the best two options, since creating and destroying materials every frame creates a garbage buildup that will need to be collected, giving you a garbage collection performance spike.

If you keep using that script the way it is now, your game will slow down to a halt and eventually crash after fading objects often.

1 Like

Thanks Thomas. This helps me out a great deal. Also I appreciate the additional information. I am still very green at scripting but, I think I understand the explanation. Thanks again.

1 Like

Wouldn’t a shader be more appropriate for this job?

Yeah, considering the shader actually has to handle the fading anyway.