Call a function on Object's Texture change!

Hi,

I want to call a function on a change of my current gameObject attached. I have a sphere as a gameObject which has two scripts TextureManager and Audio Manager. In my Audio Manager I have a three audio clips and a audio source to play clips as the texture changes in my sphere gameobject.

Here’s my AudioManager.cs script:

public class AudioManager : MonoBehaviour {

    AudioSource source;

    public AudioClip citySoundClip;
    public AudioClip restuarantSoundClip;
    public AudioClip parkSoundClip;

    public string textureName;

	void Start () 
    {
        source = GetComponent<AudioSource>();
	}


    void Update ()
    {
        textureName = gameObject.GetComponent<Renderer>().material.mainTexture.name;
        AudioManagerFunction();
    }
  
    //This code enables music one-time only till isPlaying is enabled (not working).

    public void AudioManagerFunction()
    {
        if ( textureName == "SphereTex01")
        {
            source.PlayOneShot(restuarantSoundClip);
            Debug.Log("Playing sound on : " + textureName);

        }

        if (!source.isPlaying && textureName == "SphereTex03")
        {
            source.PlayOneShot(parkSoundClip);
            Debug.Log("Playing sound on : " + textureName);
        }

        if (!source.isPlaying && textureName == "SphereTex02")
        {
            source.PlayOneShot(citySoundClip);
            Debug.Log("Playing sound on : " + textureName);
        }
    }
}

This Script is attached with my GameObject Sphere, Inspector View:
82762-inspectorview.png

What I want :

  1. To Play Audio right after the change of the texture of my gameobject.
  2. Play audio one time only, after change of texture, and again similarly with the other textures as well, play the specified music in the code I put.

PROBLEM:

The problem is in this code:

    public void AudioManagerFunction()
    {
        if ( textureName == "SphereTex01")
        {
            source.PlayOneShot(restuarantSoundClip);
            Debug.Log("Playing sound on : " + textureName);

        }

        if (!source.isPlaying && textureName == "SphereTex03")
        {
            source.PlayOneShot(parkSoundClip);
            Debug.Log("Playing sound on : " + textureName);
        }

        if (!source.isPlaying && textureName == "SphereTex02")
        {
            source.PlayOneShot(citySoundClip);
            Debug.Log("Playing sound on : " + textureName);
        }
    }

The code does work in the first call in the update( ), but it does not in the second frame of the scene. The sound of the “SphereTex01” (see code) does play audio for the first time, but after I change texture from runtime which is a button, the audio of the the second texture which is “SphereTex02” continously calls and create tons of sounds clips in game, and does not stop, like goes into a loop.

You may want to try something like: -

string previousTextureName = string.Empty;;

public void AudiomanagerFunction()
{
  if(source.IsPlaying)  return;

if(previousTextureName != textureName)
{
   if(textureName == "SphereTex01")
   {
    source.PlayOneShot( clip1);
   }
   else if(textureName == "SphereTex02")
   {
      source.PlayOneShot( clip2);
   }
previousTextureName = textureName;
}

}

I havnt tried this out but something like that.