- Home /
Disable shadows
Hi guys, I would like disable shadows when a button is pressed, i did this, but for my case, don't work. How can I disable shadows? Thank you in advance.
var buttonstyle : GUISkin ;
function Start () {
}
function OnGUI (){
GUI.skin = buttonstyle;
if(GUI.Button(new Rect(100,200,200,70),"")){
renderer.castShadows = false;
}
}
This should work fine for turning off the castShadow from the gameobject this is attached to. Are you saying it did not, or you asking how to turn off all shadows in the entire scene?
If you are using a directional light:
https://docs.unity3d.com/Documentation/ScriptReference/LightShadows.None.html
Answer by Simon-Larsen · Mar 24, 2014 at 07:49 PM
If you've baked the lights in your scenes, then this will not work. But what you can do it fetch all the lights in your scene. Warning: This may be a heavy action and make your game lag, so I've put it in the Start function, which means it caches your lights before you actually start playing. (Having tested the code, so let me know if it gives you errors)
import System.Collections.Generic;
var lightDict = new Dictionary.<Light,LightShadows>();
function Start() {
for (var light : Light in FindObjectsOfType(Light) as Light[]) {
lightDict[light] = light.shadows;
}
}
function OnGUI (){
if(GUI.Button(new Rect(100,200,200,70),"")){
for (var light in lightDict.Keys) {
if (light.shadows == LightShadows.None)
light.shadows = lightDict[light];
else
light.shadows = LightShadows.None;
}
}
}
This work, but one time that i disabled the shadows, if i re-click on the button, they don't appear anymore.. i would like that if i click, they disappear, but if i click again the button they appear.
Sorry for my english D:
I changed up the code a bit for you. It will now disable and re-enable all of your shadows as you click the button
Your answer
![](https://koobas.hobune.stream/wayback/20220613141007im_/https://answers.unity.com/themes/thub/images/avi.jpg)