0

So I have a day night cycle scripts that control the sun and skybox at different time of day. My issue is skybox is linked to each scene individually unlike the sun, so I can't manage it on a game master object with Don't Destroy Onload. Eg. at noon the skybox is supposed to change which does change but if you go to the next scene (where the change did not take place in) the skybox of the next scene is not going to respond to the change, so it will remain default skybox. Here is the part of the script that handles the sun and skybox change:

public void SunOut()
    {
        if (GameTimeManager.Time.Hour == 7)
        {
           if( GetDifference(GameTimeManager.Time, sunOutTime)== 7) //at 7:07AM sun comes out
            {
                GetComponent<Light>().intensity = 1.5f;
                Debug.Log("Sun comes out");
                
                RenderSettings.skybox = morningSkybox;


            }
        }
    }

    public void Noon()
    {
        if (GameTimeManager.Time.Hour == 12)
        {
            if (GetDifference(GameTimeManager.Time, noonTime ) == 1) //at 12:01
            {
                GetComponent<Light>().intensity = 2f;
                RenderSettings.skybox = noonSkybox;

            }

        }
    }
DataLearner555
  • 83
  • 1
  • 3
  • 9

1 Answers1

0

You can setup an on scene loaded event according to the docs here: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html. Then in that code get the skybox to check the state it should be in and change to the proper setup if necessary.

Natalia Muray
  • 252
  • 1
  • 6
  • Thanks for the reply! So you mean I should set up an enum or a list that store each skybox state? – DataLearner555 Jan 25 '22 at 08:45
  • 1
    I mean run the SunOut or Noon code on your skybox depending on the in game time on level load to fix up the skybox appearance. You can store the state as enum too if you like. If the time of day calculations are complex then it is a good idea to do so. – Natalia Muray Jan 25 '22 at 09:55
  • I found a much easier workaround by just doing a time range instead of having the change take place in a specific minute `public void SunOut() { if (GameTimeManager.Time.Hour >= 7 && GameTimeManager.Time.Hour <12) { GetComponent().intensity = 1.5f; Debug.Log("Sun comes out"); RenderSettings.skybox = morningSkybox; } }` – DataLearner555 Jan 25 '22 at 21:10