RenderSettings.Fog & Skybox not changing in Update, but work on start. (JS)

Hi there guys,

This is my storm script-

var normalWind : GameObject;
var stormWind : GameObject;
var Sun : GameObject;
var timeSinceLastCall : float;

//The scene's default fog settings
private var defaultFog;
private var defaultFogColor;
private var defaultFogDensity;
private var defaultSkybox;

defaultFog = RenderSettings.fog;
defaultFogColor = RenderSettings.fogColor;
defaultFogDensity = RenderSettings.fogDensity;
defaultSkybox = RenderSettings.skybox;
var noSkybox : Material;

function Start () {
	stormWind.SetActive(false);
	normalWind.SetActive(true);

	//Set the background color
	camera.backgroundColor = Color (0.1, 0.6, 0.7, 0.5);
}

function Update () {

	timeSinceLastCall += Time.deltaTime;
	
	if(timeSinceLastCall >= 1000){
		Storm();
	}
	if(timeSinceLastCall >= 1100){
		Debug.Log("Storm has ended");
		timeSinceLastCall = 0;
	}
	else {
		stormWind.SetActive(false);
		RenderSettings.fog = defaultFog;
		RenderSettings.fogColor = defaultFogColor;
		RenderSettings.fogDensity = defaultFogDensity;
		RenderSettings.skybox = defaultSkybox;
	}
}

function Storm () {
	RenderSettings.fog = true;
	RenderSettings.fogColor = Color (0.2, 0.25, 0.3, 0.2);
	RenderSettings.fogDensity = 0.025;
	RenderSettings.skybox = noSkybox;
	Debug.Log("Storm has begun");
	stormWind.SetActive(true);
	Sun.light.intensity = 0.25;
}

If I put the following in the start function, then I see what I hoped to see if the weather value is above 1000:
RenderSettings.fog = true;
RenderSettings.fogColor = Color (0.2, 0.25, 0.3, 0.2);
RenderSettings.fogDensity = 0.025;
RenderSettings.skybox = noSkybox;
Debug.Log(“Storm has begun”);
stormWind.SetActive(true);
Sun.light.intensity = 0.25;

However in the current situation, and i’ve rearranged it to be inside its own function and in the update, the light intensity of my directional light and wind script do change when the storm is active but the render settings (fog and skybox) DO NOT change, at all.

I have an underwater script that has identical variables with Render Settings and DOES change when the player goes underwater, but for some reason this isn’t working here (and I have disabled that Script to ensure it does not interfere with this one). Is it possible that merely having the script within the assets but not active in the scene will only allow me to fetch and change the render settings once? Should I merge the underwater and weather scripts? I thought it best I ask here before tearing up my project and causing damage inadvertently.

What have I missed here?

Kind regards,
Liam

Hi Liam,

The problem is actually in your code. In your Update function RenderSettings.fog is set to “defaultFog”, which I suppose is false. So there should be an if/else statement on that specific line preventing it from taking priority. Something like the following:

if(storm)
{
       RenderSettings.fog = true;
       RenderSettings.fogColor = Color (0.2, 0.25, 0.3, 0.2);
       RenderSettings.fogDensity = 0.025;
       RenderSettings.skybox = noSkybox;
       Debug.Log("Storm has begun");
       stormWind.SetActive(true);
       Sun.light.intensity = 0.25;
}
else
{
       stormWind.SetActive(false);
       RenderSettings.fog = defaultFog;
       RenderSettings.fogColor = defaultFogColor;
       RenderSettings.fogDensity = defaultFogDensity;
       RenderSettings.skybox = defaultSkybox;
}

Also, if you have an underwater script with a “RenderSettings.fog = defaultFog;” line like that, I suppose you might want to revisit it as well.

Cheers!