Runtime material control
This example changes Clouds Output (_CloudsOutput) on one Terrestrial body. It takes ownership of one material slot while the component is enabled, then restores the previous assignment and destroys its copy when disabled.
- Place two instances of
Planet_Terrestrial_01in a scene and leave their original body materials assigned. - Save the script as
PlanetCloudAmount.csin your own Scripts folder. Add the component to the first planet. - In the component, drag the root Mesh Renderer into Surface. Set Material Slot to
0, which corresponds to Element 0 on this prefab. Set Amount to0.25. - Enter Play Mode. The first planet should show less visible cloud color than the second. Uncheck the component to restore the original assignment, or call
SetAmountfrom your own gameplay code to vary the cloud color while it is enabled.
using UnityEngine;
public sealed class PlanetCloudAmount : MonoBehaviour{ [SerializeField] private Renderer surface; [SerializeField, Min(0)] private int materialSlot; [SerializeField, Range(0f, 1f)] private float amount = 0.25f;
private static readonly int CloudsOutput = Shader.PropertyToID("_CloudsOutput");
private Renderer owner; private int ownedSlot; private Material original; private Material copy;
private void OnEnable() { if (!surface) return;
Material[] slots = surface.sharedMaterials; if (materialSlot < 0 || materialSlot >= slots.Length) return;
Material source = slots[materialSlot]; if (!source || !source.HasProperty(CloudsOutput)) return;
owner = surface; ownedSlot = materialSlot; original = source; copy = new Material(original); SetAmount(amount); slots[ownedSlot] = copy; owner.sharedMaterials = slots; }
public void SetAmount(float value) { amount = Mathf.Clamp01(value); if (copy) copy.SetFloat(CloudsOutput, amount); }
private void OnDisable() { if (!copy) return;
if (owner) { Material[] slots = owner.sharedMaterials; if (ownedSlot < slots.Length && slots[ownedSlot] == copy) { slots[ownedSlot] = original; owner.sharedMaterials = slots; } }
Destroy(copy); copy = null; original = null; owner = null; }}sharedMaterials returns an array of assignments. Put the copy in the desired index and assign the array back to the renderer. Use one owner per slot and keep the renderer and slot selection fixed while enabled. For other controls, use a setter that matches the property type, such as SetColor for CityLight Color. The Shader Reference lists the exact property IDs.
Clouds Output changes visible cloud color. To change the separate ground shadow or highlight mask, use Cloud Shadows or Cloud Occlusion. See Clouds and cloud shadows.