Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 ritzkeen · Aug 03, 2019 at 03:01 PM · audiorandom.rangearraylistremoveat

Play a list of non-repetitive randomized audio with replay function.

The problem with this code is I'm unable to replay the current audio using the ReplayWord() function. It will however play the very next audio in list. Debug shows the same index number. Please help!


 public class ShuffleAudioCircle : MonoBehaviour
 {
   public GameObject winPopup;
   List<GameObject> audioList = new List<GameObject>();
   int randomIndex;
   void Start()
   {
     winPopup.SetActive(false);
     foreach (Transform t in transform)
     {
       audioList.Add(t.gameObject);
       t.gameObject.GetComponent<CircleCollider2D>().enabled = false;
     }
   }
   void PlayWord()
   {
     randomIndex = Random.Range(0, audioList.Count);
     Debug.Log(randomIndex);
     if (audioList.Count > 0)
     {
       audioList[randomIndex].GetComponent<AudioSource>().Play();
       audioList[randomIndex].GetComponent<CircleCollider2D>().enabled = true;
     }
     else
     {
       winPopup.SetActive(true);
     }
     audioList.RemoveAt(randomIndex);
   }
   public void ReplayWord()
   {
     Debug.Log(randomIndex);
     audioList[randomIndex].GetComponent<AudioSource>().Play();
   }
 }
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

2 Replies

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

Answer by Hellium · Aug 03, 2019 at 03:15 PM

You are removing the gameObject after playing the song, so you can't retrieve it afterwards.

  public class ShuffleAudioCircle : MonoBehaviour
  {
    public GameObject winPopup;
    List<GameObject> audioList = new List<GameObject>();
    int randomIndex = -1;
    void Start()
    {
      winPopup.SetActive(false);
      foreach (Transform t in transform)
      {
        audioList.Add(t.gameObject);
        t.gameObject.GetComponent<CircleCollider2D>().enabled = false;
      }
    }
    void PlayWord()
    {
      if (audioList.Count > 0 && randomIndex >= 0 )
            audioList.RemoveAt(randomIndex);

      if (audioList.Count > 0)
      {
        randomIndex = Random.Range(0, audioList.Count);
        Debug.Log(randomIndex);
        audioList[randomIndex].GetComponent<AudioSource>().Play();
        audioList[randomIndex].GetComponent<CircleCollider2D>().enabled = true;
      }
      else
      {
        winPopup.SetActive(true);
      }
    }
    public void ReplayWord()
    {
       if( audioList.Count > 0 && randomIndex >= 0 )
       {
          Debug.Log(randomIndex);
          audioList[randomIndex].GetComponent<AudioSource>().Play();
       }
    }
  }

Comment
Add comment · Show 4 · 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 ritzkeen · Aug 03, 2019 at 03:33 PM 0
Share

Thank man. It work!

avatar image ritzkeen · Aug 03, 2019 at 04:30 PM 0
Share

However after testing till finish. I've just realized that the winPopup.SetActive; function is broken. The log as per below:

 ArgumentOutOfRangeException: Index was out of range. $$anonymous$$ust be non-negative and less than the size of the collection.

Too tired to continue now.. :-'( Thanks again for your help.

avatar image Hellium ritzkeen · Aug 03, 2019 at 06:40 PM 0
Share

Give a try to the code in my answer I've fixed (PlayWorld function basically)

avatar image ritzkeen Hellium · Aug 04, 2019 at 12:39 AM 0
Share

awwww. mann... it work! thank you!

avatar image
0

Answer by rodude123 · Aug 03, 2019 at 03:12 PM

Your PlayWord method is not being used anywhere try and use it in the update or fixedUpdate method. Lastly your for each loop

 foreach (Transform t in transform)
 {
     audioList.Add(t.gameObject);
     t.gameObject.GetComponent<CircleCollider2D>().enabled = false;
 }

doesn't make any sense, what is transform

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 ritzkeen · Aug 03, 2019 at 03:23 PM 0
Share

The script is hooked up to an empty gameobject with a list of gameobjects with audiosource. A button is used to trigger the Playword() function.

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

141 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

Related Questions

Problems with Arrays 2 Answers

Looking for a method for implementing multiple random sound arrays. 2 Answers

Start ambient audio tracks at random points? 2 Answers

AudioSource Array Index is out of range 3 Answers

How can I randomly create a number and then delete it to stop repeating 3 Answers


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