Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Strange Array Problem

Discussion in 'Scripting' started by elcuci, Mar 30, 2023.

  1. elcuci

    elcuci

    Joined:
    Jun 13, 2021
    Posts:
    17
    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!

    Code (CSharp):
    1.     IEnumerator PlayFootstepFromRenderer(Renderer Renderer)
    2.     {
    3.         if (stepCycleProgress > distanceBetweenSteps)
    4.         {
    5.             stepCycleProgress = 0f;
    6.  
    7.             foreach (TextureSound textureSound in TextureSounds)
    8.             {
    9.                 if (textureSound.Albedo == Renderer.material.GetTexture("_MainTex"))
    10.                 {
    11.                     texInList = true;
    12.                     AudioClip clip = GetClipFromTextureSound(textureSound);
    13.  
    14.                     audioSource.PlayOneShot(clip);
    15.  
    16.                    
    17.                     //texInList = false;
    18.                     yield return new WaitForSeconds(clip.length);
    19.                     break;
    20.                 }
    21.                 if (!texInList)
    22.                 {
    23.                     AudioClip clip = GetClipNullClips();
    24.  
    25.                     audioSource.PlayOneShot(clip);
    26.                     yield return new WaitForSeconds(clip.length);
    27.                     break;
    28.  
    29.                 }
    30.  
    31.             }
    32.         }
    33.  
    34.     }
    35.  
    36.     private AudioClip GetClipNullClipsForJumpLand()
    37.     {
    38.         return GetClipFrom(NullClipsJumpLand);
    39.     }
    40.  
    41.     private AudioClip GetClipFrom(AudioClip[] clips)
    42.     {
    43.         int clipIndex = Random.Range(1, clips.Length);
    44.  
    45.         AudioClip temporary = clips[clipIndex];
    46.         clips[clipIndex] = clips[0];
    47.         clips[0] = temporary;
    48.  
    49.         return temporary;
    50.  
    51.     [System.Serializable]
    52.     private class TextureSound
    53.     {
    54.         public Texture Albedo;
    55.         public AudioClip[] Clips;
    56.         public AudioClip[] JumpClips;
    57.  
    58.     }
    59.     }
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,070
    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.
     
  3. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,061
    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.