- Home /
The question is answered, right answer was accepted
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:
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.
Answer by Gmotagi · Nov 24, 2016 at 03:20 PM
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.
Thanks for the answer. I did managed to get it after tweaking my code and yours a little to get what I want, Also I added audiosources to my object. Here's what I did, In case someone wants to know:
public void Audio$$anonymous$$anagerFunction()
{
if (previousTextureName != textureName)
{
if (textureName == "SphereTex01")
{
source1.Play();
source2.Stop();
source3.Stop();
}
else if (textureName == "SphereTex03")
{
source2.Play();
source1.Stop();
source3.Stop();
}
else
{
source3.Play();
source1.Stop();
source2.Stop();
}
previousTextureName = textureName;
}
}