Keep track of last audio clip played from multiple arrays
I'm not sure if there is a way to do this but basically I have all of my voices being called from a script that holds multiple arrays one for each audio response. I'm wondering is there a way in C# to track the last audio clip played so I could access that from another script to use in an if statement condition check. Something like:
Sudo Code: if (lastClipPlayed == VoiceBox.IdontKnowRespClip){ Do Something }
What is your current code? Can you not just use a public variable?
Thanks @ joshDangit, I just posted a response to @roman_sedition. ;) I hope I'm explain it right? ;)
Basically I have a script on an object I call "VoiceBox" This hold numerous arrays for audio responses.
So I want to reference some sort of variable that holds the last clip played from any one of those arrays that I can call in another script and use it to see if fine tune the type of response.
Something like: lets say you say: "What time is it?"
And the possible responses can be anything but you want to do something like:
if (LastResponseWas == Idont$$anonymous$$nowResClip){
Do Something
}
probably just assign it to a public Audiosource after you've played it.
public Audiosource lastClipPlayed;
void PlayClip(Audiosource clip)
{
clip.Play();
lastClipPlayed = clip;
}
Thanks @roman_sedition kinda but I need to be able to I guess hold that variable so I could have an audio clip played from one of the many response arrays, hold that last clip played name somehow maybe as a public variable/string in the inspector and then reference that in another script something like:
if (LastRespClip == Idont$$anonymous$$nowRespClip){
Do Something
}
Not sure if that makes more sense?
The confusing part is " I want to in the inspector assign a public variable", I'm not sure quite what you mean by that.
but let's say that 'ScriptA', is the class which holds your BillyThe$$anonymous$$iddFactoid method and it is on GameObjectA.
and 'ScriptB ' has your if statement and it's on GameObjectB
In ScriptA, you just declare a public AudioClip variable
public AudioClip lastClipPlayed;
then you just change the BillyThe$$anonymous$$iddFactoid to be
public void BillyThe$$anonymous$$iddFactoid(){
AudioClip myClip = BillyThe$$anonymous$$iddClips [Random.Range (0, BillyThe$$anonymous$$iddClips.Length)];
source.PlayOneShot (myClip);
lastClipPlayed = myClip;
StartCoroutine (voiceBlocker (myClip.length + AdditionalDelay));
}
then your If statement to access ScriptA's public variable of lastClipPlayed would look like
if (GameObjectA.GetComponent<ScriptA>().lastClipPlayed == $$anonymous$$ITT_VoiceBox.Idont$$anonymous$$nowRespClip){
Do Something
}
Answer by roman_sedition · Nov 16, 2016 at 10:56 PM
This is how you can access the public variable on one script from another script.
Let's say that 'ScriptA', is the class which holds your BillyTheKiddFactoid method and it is on GameObjectA.
and 'ScriptB ' has your if statement and it's on GameObjectB
In ScriptA, you just declare a public AudioClip variable
public AudioClip lastClipPlayed;
then you just change the BillyTheKiddFactoid to be
public void BillyTheKiddFactoid(){
AudioClip myClip = BillyTheKiddClips [Random.Range (0, BillyTheKiddClips.Length)];
source.PlayOneShot (myClip);
lastClipPlayed = myClip;
StartCoroutine (voiceBlocker (myClip.length + AdditionalDelay));
}
then your If statement to access ScriptA's public variable of lastClipPlayed would look like
if (GameObjectA.GetComponent<ScriptA>().lastClipPlayed == KITT_VoiceBox.IdontKnowRespClip){
Do Something
}
Thanks @roman_sedition, I found I only had to make one small change but other than that this was correct and got me on the right track, thank you again very kindly for the point in the right direction. This was what I had to make the change too:
if ($$anonymous$$ITTvoiceBox.GetComponent<VoiceBox> ().lastClipPlayed.name == "40. Co$$anonymous$$g up $$anonymous$$ichael"){
Do Something
}
As you can see because I guess the audio clip name is a string I had to use lastClipPlayed.name == "Actual name of Audio Clip"
But it worked :)
Your answer
Follow this Question
Related Questions
Determine Audio Clip Length From An Array And Then Add A Delay 1 Answer
Change max range attribute? 0 Answers
C# ArrayList Accessing and RemoveAt? 0 Answers
Take 2: Audio silent in Play Mode. 0 Answers
Arrays with multiple data types C# 1 Answer