- Home /
Massive FPS hit when changing colors on the fly
Hi guys,
the following line of code causes a drop of 40 in FPS. It is called every frame.
Obj.renderer.material.color = new Color (1-varCurrent/varMax,0,varCurrent/varMax,0.5f);
Can anyone see right away why this is happening or do I need to post additional Information?
thank you for any help
Answer by MrSoad · Oct 11, 2014 at 07:13 PM
I can't see any obvious issue with that code. You are not doing any Debug.Log stuff to check values at all are you? This would hit your frame rate very hard if you are doing it every frame. Could do with seeing more code as this should not give you any noticeable hit even if called every frame. My laptop is Ancient and would run this fine all day long.
Answer by robertbu · Oct 11, 2014 at 04:01 PM
The first time you change any material property, a new material instance is generated for that object. The idea is that any changes you make to an object's material at runtime will only apply to that object. I would only expect a significant FPS drop from this issue if you had a lot of objects. This may or may not be your issue. That is if you call the above line in Start() for all your object (i.e. just once not every frame), then I would expect to see the FPS drop even if the line is not called every frame.
If you are seeing a FPS drop only if this is called every frame, then my material instance idea is likely wrong.
If this is your issue than assign the $$anonymous$$aterial to a variable. Then Assign that stored material to all your objects. Then they will all share the same one material which you can then update the color of without any issues.
Answer by winexploder · Oct 11, 2014 at 07:07 PM
This line was apparently not the culprit. After taking Vector3(0,0,0) and Queternion.identity out of the Obj Instantiate it worked fine.