How do I change render.material at runtime seamlessly?

I have an enemy death effect that requires a different material. When I set the renderer.material to a new material (happens in an animation event callback), I get a few frames of nothing being rendered for the mesh before it reappears with the new material. I’d like it to be continuous, i.e. one render frame it’s the old material and the next it’s the new one.

Is there a solution or workaround? Am I just going about this the wrong way?

I’ve swapped around plenty of materials, and never seen anything like that. Even if it was some sort of loading issue, there would be a frame drop - it wouldn’t just run the game without things loaded. And even if it did, I think the object would be pink, not invisible.

My guess is it’s a completely unrelated problem (but maybe triggered somehow by your changing the material, or not.)

Is there anything odd about the new material? Crazy custom shader with lots of moving vars? Does it happen with even a simple (diffuse blue) material?

My guess is that the texture belonging to the material you want to display is not already used somewhere else. Therefore Unity has to load the texture, which takes a while.

You could try to preload the material when you are starting the game/level. I would test it by simply assigning the material to a cube, placed offscreen where it’s not seen by the player. That way the texture will already be loaded into memory and switching materials should be instant.

I haven’t tried this, but it’s not that complicated to do so =)