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 /
avatar image
0
Question by KnightRiderGuy · Jan 12, 2017 at 09:01 PM · c#audioplayerprefsididentifier

Give Audio Clip From Array A Unique I.D.

Is there a way to give an audio clip from a specific array of audio clips a unique I.D. that I could reference from other clips or save to player prefs?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Glurth · Jan 12, 2017 at 10:07 PM

I like static classes for stuff like that (uncompiled/tested example). Note: the counter will reset to Zero every time you launch the program, so this won't do for SAVING the ID between sessions. But if you give them ID's onload or at runtime only, this should do.

 static public class AutoClipCounter{
    static int counter=0;
    public static int GetNextID() {return counter++;}
 }

Access with something like;

 clipID=AudioClipCounter.GetNextID();
Comment
Add comment · Show 2 · 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 · Jan 13, 2017 at 02:41 PM 0
Share

Thanks @Glurth, That might work, so far to track the last audio clip played I have been using something like this:

 public void ThatReferenceEscapes$$anonymous$$e(){
         AudioClip myClip = ThatReferenceEscapes$$anonymous$$eClips [Random.Range (0, ThatReferenceEscapes$$anonymous$$eClips.Length)];
         source.PlayOneShot (myClip);
         lastClipPlayed = myClip;
         StartCoroutine (voiceBlocker (myClip.length + AdditionalDelay));
     }

And then using the: lastClipPlayed to access the name of the clip using .name

I had thought of saving the .name of the last clip played as a string into a temporary player prefs....?? $$anonymous$$aybe your idea might be better....?? Not sure??

avatar image Glurth KnightRiderGuy · Jan 13, 2017 at 05:53 PM 0
Share

Well, if your trying to get the "next clip" randomly, then I don't think my solution will help, since it will always give you the clips in order. I think in this case, you just need to make the lastClipPlayed as STATIC variable. (only one instance of a static variable, regardless of how many class instances exist.) You would also access it by class Name. The uniqueID number, will be equal to the index into the array.

 public Class ClipPlayer{
 public static int LastAudioIndex; 
 
    public void ThatReferenceEscapes$$anonymous$$e(){
          int newIndex=Random.Range (0, ThatReferenceEscapes$$anonymous$$eClips.Length);
          while(newIndex==LastAudioIndex) //example: don't repeat last clip code
              newIndex=Random.Range (0, ThatReferenceEscapes$$anonymous$$eClips.Length);
          AudioClip myClip = ThatReferenceEscapes$$anonymous$$eClips [newIndex];
          LastAudioIndex=newIndex
          source.PlayOneShot (myClip);
          lastClipPlayed = myClip;
          StartCoroutine (voiceBlocker (myClip.length + AdditionalDelay));
      }
 }
 ....

in a different class, you can access that variable via

 ClipPlayer.LastAudioIndex

You can certainly save&load this value as a player pref, which will allow it to store the value between sessions. If you don't care what the lastClipPayed was in the previous session, then no need to save it as a player pref.

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

279 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 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

Distribute terrain in zones 3 Answers

Getting Xbox 360 User ID (Or some sort of unique identifier) 0 Answers

Multiple Cars not working 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Saving final score and displaying on main menu 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