- Home /
Question by
ElianWonhalf · Jan 26, 2014 at 02:40 AM ·
c#changeskybox
Skybox not changing (through code)
I tried to change the skybox through the code, and only the "normal" skybox is applied in game. The other one isn't... Here is the code :
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Underwater : MonoBehaviour {
private float waterLevel;
private GameObject water;
private bool isUnderwater;
public Color normalColor;
public Color underwaterColor;
public Material normalSky;
public Material underwaterSky;
// Use this for initialization
void Start () {
water = GameObject.Find ("Daylight Water");
waterLevel = water.transform.position.y;
normalColor = new Color (0.5f, 0.5f, 0.5f, 0.5f);
underwaterColor = new Color (0.22f, 0.50f, 0.77f, 0.5f);
RenderSettings.fog = true;
SetNormal ();
}
// Update is called once per frame
void Update () {
if ((transform.position.y < waterLevel) != isUnderwater) {
isUnderwater = transform.position.y < waterLevel;
if(isUnderwater) SetUnderwater ();
else SetNormal ();
}
}
void SetUnderwater () {
RenderSettings.fogColor = underwaterColor;
RenderSettings.fogDensity = 0.03f;
water.transform.rotation = Quaternion.Euler(180f, 0, 0);
RenderSettings.skybox = underwaterSky;
}
void SetNormal () {
RenderSettings.fogColor = normalColor;
RenderSettings.fogDensity = 0.002f;
water.transform.rotation = Quaternion.Euler(0, 0, 0);
RenderSettings.skybox = normalSky;
}
}
I'm a newbie in Unity and with this website, btw. Thanks a lot.
Comment
Throw a debug into the SetUnderwater() method - does it get there?
I know it gets there because the fog comes when I go underwater, but the skybox... Nope, nothing :/ ...
Your answer
Follow this Question
Related Questions
Can't make a Skybox 0 Answers
How do I change the startcolor.r in code? 2 Answers
How to: Create shader and copy texture? 2 Answers
targetting dont work 0 Answers
AssetBundles coding 1 Answer