Changing Skybox's texture via code.


I’ve read about how to change Skybox’s texture via code. I’m interested to change RenderSetting’s one.

I googled a lot and tried whatever I could find, from this:

RenderSettings.skybox.mainTexture = myTexture;


//				RenderSettings.skybox.SetTexture("_FrontTex", textures[kos]);
//				RenderSettings.skybox.SetTexture("_BackTex", textures[kos]);
//				RenderSettings.skybox.SetTexture("_LeftTex", textures[kos]);
//				RenderSettings.skybox.SetTexture("_RightTex", textures[kos]);
//				RenderSettings.skybox.SetTexture("_UpTex", textures[kos]);
//				RenderSettings.skybox.SetTexture("_DownTex", textures[kos]);


Camera.main.GetComponent<Skybox>().material.SetTexture("_FrontTex", textures[kos]);
				Camera.main.GetComponent<Skybox>().material.SetTexture("_BackTex", textures[kos]);
				Camera.main.GetComponent<Skybox>().material.SetTexture("_LeftTex", textures[kos]);
				Camera.main.GetComponent<Skybox>().material.SetTexture("_RightTex", textures[kos]);
				Camera.main.GetComponent<Skybox>().material.SetTexture("_UpTex", textures[kos]);
				Camera.main.GetComponent<Skybox>().material.SetTexture("_DownTex", textures[kos]);

But none of them changed it. What am I missing?


Reworked my solution. This version will allow you to use whatever textures you want. Honestly its a little rough and has some superfluous struct action, but it works, and I’m not going to spend any more time on it. :slight_smile:

using UnityEngine;
using System.Collections;

public class SkyboxSetter : MonoBehaviour
	public static Material CreateSkyboxMaterial(SkyboxManifest manifest)
		Material result = new Material(Shader.Find("RenderFX/Skybox"));
		result.SetTexture("_FrontTex", manifest.textures[0]);
		result.SetTexture("_BackTex", manifest.textures[1]);
		result.SetTexture("_LeftTex", manifest.textures[2]);
		result.SetTexture("_RightTex", manifest.textures[3]);
		result.SetTexture("_UpTex", manifest.textures[4]);
		result.SetTexture("_DownTex", manifest.textures[5]);
		return result;

	public Texture2D[] textures;

	void OnEnable()
		SkyboxManifest manifest = new SkyboxManifest(textures[0], textures[1], textures[2], textures[3], textures[4], textures[5]);
		Material material = CreateSkyboxMaterial(manifest);
		enabled = false;

	void SetSkybox(Material material)
		GameObject camera = Camera.main.gameObject;
		Skybox skybox = camera.GetComponent<Skybox>();
		if (skybox == null)
			skybox = camera.AddComponent<Skybox>();
		skybox.material = material;

public struct SkyboxManifest
	public Texture2D[] textures;

	public SkyboxManifest(Texture2D front, Texture2D back, Texture2D left, Texture2D right, Texture2D up, Texture2D down)
		textures = new Texture2D[6]

The link below from the unity wiki may help you.

It blends 2 sky boxes. You just need it to blend them real fast from your 1st skybox to the one you want to replace it with.

Hope it helps.

Link to wiki document here.