Search Unity

Setting up a single button to switch between night and day

Discussion in 'Editor & General Support' started by bjdobson1, Jun 9, 2020.

  1. bjdobson1

    bjdobson1

    Joined:
    Apr 25, 2018
    Posts:
    7
    Hello. I got this old VRChat world I'm co-owning with a friend of mine (it's their world yet I've been doing most of everything in it). Though my friend managed to get it into Unity 2018.4.20f1.
    Now I want to be able to have a button to toggle day and night. Having two Directional Lights (one 'Night' and one 'Day'). But the problem is there's only one Lighting.. thingy? I'm hearing you can cycle this with coding and I've been trying to look around for something, but I'm not really finding what I specifically want (all I see is for realtime day/night cycling, not a button to make it instant day/night).

    If anyone knows what I'm talking about, could I please get hopeful step-by-step help in setting this up? My friend doesn't know how to set it up and suggested I asked here.
     
  2. bjdobson1

    bjdobson1

    Joined:
    Apr 25, 2018
    Posts:
    7
    *Bump* ..Could I please get some help?
     
  3. Marc-Saubion

    Marc-Saubion

    Joined:
    Jul 6, 2011
    Posts:
    655
    Just activate and deactivate your different suns. It should work.
     
  4. bjdobson1

    bjdobson1

    Joined:
    Apr 25, 2018
    Posts:
    7
    It doesn't change the skybox lighting, I tried this.
    I want to be able to switch the skybox lighting and the skybox itself when the toggle is hit. I heard it's possible with scripting, but I haven't found anything for toggling.. only transitioning from day to night.
     
    Last edited: Jun 12, 2020
  5. bjdobson1

    bjdobson1

    Joined:
    Apr 25, 2018
    Posts:
    7
    *bump*
    Anyone?
     
  6. bjdobson1

    bjdobson1

    Joined:
    Apr 25, 2018
    Posts:
    7
    ...Hello?
     
  7. FurkanSz

    FurkanSz

    Joined:
    Dec 27, 2019
    Posts:
    9
    Just have 2 references to your different directional lights game objects in your script. One for your sun and other for a moon. Also have 2 materials that are your daytime skybox and nighttime skybox.

    Then when you want to toggle to one just do something like this:
    Code (CSharp):
    1.  
    2. public void ToggleNight(){
    3. sun.SetActive(!sun.activeSelf);
    4. moon.SetActive(!moon.activeSelf);
    5. if(moon.activeSelf){
    6. RenderSettings.skybox = nightTimeSkybox;
    7. }else{
    8. RenderSettings.skybox = dayTimeSkybox;
    9. }
    10. DynamicGI.UpdateEnvironment();
    11. }
    12.  
    DynamicGI call is important and required when you swap skyboxes. Hope this helps.
     
  8. bjdobson1

    bjdobson1

    Joined:
    Apr 25, 2018
    Posts:
    7
    Hey. I'm not sure what you mean by all of this. Perhaps this picture might help?
    upload_2020-6-22_16-13-21.png
    I have this 'DynamicGl' installed, dunno how to use it. Now then, both Directional lights (Day and Night) are pointing down, and the 'Moon' is literally just a sphere with a texture.
    I have set up a Day and Night material, but I dunno where to put the script or even how to set it up.
     
    Last edited: Jun 23, 2020
  9. bjdobson1

    bjdobson1

    Joined:
    Apr 25, 2018
    Posts:
    7
    *bump*
    Could I get some help with the above response, please?