Strange Array Problem

Hello! Question: When I put a texture as the first element (element 0) in the texture sound array or the elements preceeding it have no texture assigned, it recognizes it and plays the corresponding sound, however when it is not the first element (element 0), it will go straight to GetClipNullClip(), even though it shouldn’t because the texture is properly assigned. Any ideas? If I’m missing something please let me know!

    IEnumerator PlayFootstepFromRenderer(Renderer Renderer)
    {
        if (stepCycleProgress > distanceBetweenSteps)
        {
            stepCycleProgress = 0f;

            foreach (TextureSound textureSound in TextureSounds)
            {
                if (textureSound.Albedo == Renderer.material.GetTexture("_MainTex"))
                {
                    texInList = true;
                    AudioClip clip = GetClipFromTextureSound(textureSound);

                    audioSource.PlayOneShot(clip);

                   
                    //texInList = false;
                    yield return new WaitForSeconds(clip.length);
                    break;
                }
                if (!texInList)
                {
                    AudioClip clip = GetClipNullClips();

                    audioSource.PlayOneShot(clip);
                    yield return new WaitForSeconds(clip.length);
                    break;

                }

            }
        }

    }

    private AudioClip GetClipNullClipsForJumpLand()
    {
        return GetClipFrom(NullClipsJumpLand);
    }

    private AudioClip GetClipFrom(AudioClip[] clips)
    {
        int clipIndex = Random.Range(1, clips.Length);

        AudioClip temporary = clips[clipIndex];
        clips[clipIndex] = clips[0];
        clips[0] = temporary;

        return temporary;

    [System.Serializable]
    private class TextureSound
    {
        public Texture Albedo;
        public AudioClip[] Clips;
        public AudioClip[] JumpClips;

    }
    }

I’m sorry, I can’t understand your question at all. Array something something. But when something then nothing.

I intend this to be humorous, don’t be offended. It would be helpful if you shared either a) a contrived example of the thing that doesn’t work, or b) more information on what you’re trying to achieve.

For example, this doesn’t mean anything, the explanation is all over the place.

The if (!texInList) is inside the foreach loop, you want to move it after the loop. You also want to set texInList to false before the loop.

The way it is right now, when texInList is false, it will call GetClipNullClips, break out of the loop and never look at the rest of the array.

4 Likes