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 :slight_smile:

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;
       }
  }    

}

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.
   }
}

This shader can be of some use: SkyboxBlended.

Thanks for the good explanation! Very helpful :slight_smile:

you can see it working! Yey alt text