- Home /
Switching between skyboxes
Hi all, I am trying to switch between skyboxes in RenderSettings based on Random value but seems like my logic is not working properly. I am quite a beginner in coding.
any help would be great :)
var stars: Material; var sky: Material;
RenderSettings.skybox = stars;
function Update() {
if ( Random.value > 0.5 )
{
if (RenderSettings.skybox == sky)
{
RenderSettings.skybox = stars;
}
else
{
RenderSettings.skybox == sky;
}
}
}
Answer by BiG · Apr 25, 2012 at 09:54 AM
You've done a very little mistake, but this is relevant!!! Look at the double "=" in the following statement:
else
{
RenderSettings.skybox == sky;
}
Replace with:
else
{
RenderSettings.skybox = sky;
}
Double "=" is for a comparison, single "=" is for an assignment. But I think it was just a copy-&-paste error, since I see that you've understood this logic from the other lines.
EDIT: I also see that your code is inside an Update function, and it's executed every frame. Two problems about this: 1) it's computationally expensive for an operation of skybox changing 2) it's useless, because you couldn't have the time to see the changes, unless the same range of values is generated for a long time. Try this:
var stars: Material;
var sky: Material;
RenderSettings.skybox = stars;
function Start() {
while (true)
{
if ( Random.value > 0.5 )
{
if (RenderSettings.skybox == sky)
{
RenderSettings.skybox = stars;
}
else
{
RenderSettings.skybox = sky;
}
}
yield WaitForSeconds(1); //adjust the time between the possible change.
}
}
if adding while(true) in Start function, i think the program will be crashed
Answer by vlknff · Apr 25, 2012 at 10:50 PM
Thanks for the good explanation! Very helpful :)
you can see it working! Yey