Unity-Technologies/UnityCsReference · error · ArgumentException

index must be between 0 and {emissiveTextureCount - 1}, but

Error message

index must be between 0 and {emissiveTextureCount - 1}, but was {index}

What it means

LightBaker.GetEmissiveTexture(uint index) throws ArgumentException when index >= emissiveTextureCount. The emissive set is separate from the albedo set and may have a different count or be empty depending on whether the scene contains emissive materials. The guard mirrors GetAlbedoTexture and protects the native texture lookup.

Source

Thrown at Editor/Mono/GI/LightBaker.bindings.cs:438

        public extern ulong GetByteSize();

        public Texture2D GetAlbedoTexture(uint index)
        {
            if (index >= albedoTextureCount)
                throw new ArgumentException($"index must be between 0 and {albedoTextureCount - 1}, but was {index}");
            TextureData textureData = GetAlbedoTextureData(index);
            Texture2D tex = new((int)textureData.resolution.width, (int)textureData.resolution.height, TextureFormat.RGBAFloat, false);
            tex.SetPixelData(textureData.data, 0);
            tex.filterMode = FilterMode.Point;
            tex.Apply();
            return tex;
        }

        public Texture2D GetEmissiveTexture(uint index)
        {
            if (index >= emissiveTextureCount)
                throw new ArgumentException($"index must be between 0 and {emissiveTextureCount - 1}, but was {index}");
            TextureData textureData = GetEmissiveTextureData(index);
            Texture2D tex = new((int)textureData.resolution.width, (int)textureData.resolution.height, TextureFormat.RGBAFloat, false);
            tex.SetPixelData(textureData.data, 0);
            tex.filterMode = FilterMode.Point;
            tex.Apply();
            return tex;
        }

        static extern IntPtr Internal_Create();
        [NativeMethod(IsThreadSafe = true)]
        static extern void Internal_Destroy(IntPtr ptr);

        extern LightingSettings Internal_GetLightingSettings();
        public LightingSettings GetLightingSettings()
        {
            return Internal_GetLightingSettings();
        }
        extern void Internal_SetLightingSettings(LightingSettings lightingSettings);

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Read emissiveTextureCount independently and iterate [0, count).
  2. Treat emissiveTextureCount == 0 as 'no emissive output' rather than an error.
  3. Do not assume emissive and albedo counts match; index them separately.

Example fix

// before
var emissive = lightBaker.GetEmissiveTexture(albedoIndex);

// after
for (uint i = 0; i < lightBaker.emissiveTextureCount; i++)
    yield return lightBaker.GetEmissiveTexture(i);
Defensive patterns

Strategy: validation

Validate before calling

uint count = lightBaker.emissiveTextureCount;
if (index < count)
    var tex = lightBaker.GetEmissiveTexture(index);

Prevention

When it happens

Trigger: Assuming emissiveTextureCount equals albedoTextureCount and reusing the same loop bound; calling GetEmissiveTexture before a bake that produced emissive output.

Common situations: Scenes with no emissive materials yield emissiveTextureCount == 0; reusing an albedo-index to fetch emissive textures; stale count after re-bake.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/7eadc4a999c682e0. Report an issue: GitHub.