Search Unity

Change Fog Color at Runtime using Hex color code

Discussion in 'Scripting' started by sowatnow, Dec 2, 2017.

  1. sowatnow

    sowatnow

    Joined:
    Jun 12, 2014
    Posts:
    304
    Hi guys,

    I am trying to change Fog Color at runtime but cant' get it to work.

    The following code works when i choose one of the main color like red, green, blue etc.. but can't use it when it comes to using hexcode.

    Code (CSharp):
    1. public class ExampleClass : MonoBehaviour {
    2.     void Example() {
    3.         RenderSettings.fogColor = Color.blue;
    4.         RenderSettings.fog = true;
    5.     }
    6. }
    The color i want fog to change to at runtime is light blue and the hex color code is 4CA7E6FF.

    Is there a way i can make this to work?

    Thanks
     
  2. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,021
  3. sowatnow

    sowatnow

    Joined:
    Jun 12, 2014
    Posts:
    304

    Thanks.

    I tried the above method, but I can't seem to get it to work when i add bConverted line. I am using the attached code below to change the fogcolor and enable a skybox onTrigger with Player.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SkyboxchangeC : MonoBehaviour {
    5.  
    6.     public Material otherSkybox;
    7.  
    8.     public Color newCol;
    9.  
    10.     bool bConverted = ColorUtility.TryParseHtmlString("#4CA7E6FF", out newCol);
    11.  
    12.     void OnTriggerEnter(Collider other)
    13.     {
    14.         if (other.tag == "Player" && RenderSettings.skybox != otherSkybox && bConverted)
    15.             RenderSettings.skybox = otherSkybox;
    16.             RenderSettings.fog = true;
    17.             RenderSettings.fogColor = newCol;
    18.     }
    19. }
     
unityunity