- Home /
Can you remove lightmaps at runtime through scripts?
I am scripting a scene in my game where when the player enters a trigger all the lights go out. The script works, but rather than the room becoming completely black, you can see the light dim, but the lightmaps are left behind. Is there a way to remove these lightmaps at runtime to actually kill the lights upon reaching the trigger?
Answer by HarshadK · Jun 11, 2014 at 06:15 AM
What you are looking for is Renderer.lightmapIndex
The documentation states that
The index refers to lightmaps array in LightmapSettings class. A value of 255 means no lightmap has been assigned, which is the default.
So you can set the lightmapIndex to be 255 when you want to turn off the lightmaps.
Could you possibly explain this? I read up on it but I don't quite understand how I am supposed to use it.
This forum post about Turn "Use Lightmaps" off via script? has the explanation and code required by you.
Also check for the code by @fenderrio which uses the method of setting the data of lightmaps to null.
The problem is that it references level.Renderer.lightmapIndex, without explaining that game object which is tied to "level".
The game object named level is one of the objects which is assigned the lightmap.
But in the code below from the same forum:
function TurnLight$$anonymous$$apsOff (){
var children = level.GetComponentsInChildren (Renderer);
for (var child: Renderer in children){
originalLight$$anonymous$$apIndex = child.renderer.lightmapIndex;
Debug.Log (child.renderer.lightmapIndex);
child.renderer.lightmapIndex = -1;
}
}
It looks like all the game objects that have light map are children of an empty game object named level.