Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by KnightRiderGuy · Nov 16, 2016 at 02:29 PM · c#arraysaudiocliparraylistaudio.playoneshot

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 }

Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JoshDangIt · Nov 16, 2016 at 02:42 PM 0
Share

What is your current code? Can you not just use a public variable?

avatar image KnightRiderGuy JoshDangIt · Nov 16, 2016 at 05:09 PM 0
Share

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
 }
avatar image roman_sedition · Nov 16, 2016 at 03:50 PM 0
Share

probably just assign it to a public Audiosource after you've played it.

 public Audiosource lastClipPlayed;
 
 
 void PlayClip(Audiosource clip)
 {
         clip.Play();
         lastClipPlayed = clip;
 }
avatar image KnightRiderGuy roman_sedition · Nov 16, 2016 at 05:01 PM 0
Share

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?

avatar image roman_sedition KnightRiderGuy · Nov 16, 2016 at 06:10 PM 1
Share

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
  }



Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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
   }
 



Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KnightRiderGuy · Nov 17, 2016 at 01:45 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

239 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges