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 kab_6182 · Jun 21, 2017 at 12:28 AM · coroutineaudiosourcelooping

How to start a courine with input from keyboard

I have the code below which basically takes multiple audio sources as a game object and play the audio sources randomly one after another by removing the ones that already been played so not to repeat

             public class RanodmAudioObjects : MonoBehaviour
            {


      public List<AudioSource> sourcesInScene; //assign these in the inspector
      void Start()
 {
     StartCoroutine(SoundsTick());
 }
 IEnumerator SoundsTick()
 { //we're using an ienumerator so we can "wait" between loops.
    while (true)
     {
       for (int x = 0; x < sourcesInScene.Count; x++)
         {
             yield return null;
             x = Random.Range(0, sourcesInScene.Count);
             sourcesInScene[x].Play();
             sourcesInScene.RemoveAt(x);
             yield return new WaitForSeconds(1f);}
         yield return null;}}}

  1. have two questions, one how would I be able to incorporate a keyboard input so the coroutine only start when it gets an input from keyboard and not upon entering play mode.

2.two, As you see on my code once the loop has gone throgh all the objects it will have them be removed but I want it once the loop is over I want it to get all the audio sources again and loop randomly as previous when getting input from keyboard

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
Best Answer

Answer by Filhanteraren · Jun 21, 2017 at 02:33 AM

To get any input use this in your update method:

 if (Input.GetKeyDown(KeyCode.Space))
          {
              // Do something here;
          }
 }

You can read more about it in the documentation here:

https://docs.unity3d.com/Manual/Input.html

For your second question you could use another list to go through your indexes. And remove and reset that list when you are done.

I made a quick script here bellow that should work, not tested so be aware of that.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayRandomSound : MonoBehaviour
 {
     public List<AudioSource> sourcesInScene; //assign these in the inspector
     private List<int> indexCount = new List<int>();
 
     public void Update()
     {
         if (Input.GetKeyDown(KeyCode.Space))
         {
             ResetIndexCount(sourcesInScene.Count);
             StartCoroutine(SoundsTick());
         }
     }
 
     private void ResetIndexCount(int number)
     {
         indexCount.Clear();
         for (int i = 0; i < number; i++)
         {
             indexCount.Add(i);
         }
     }
 
     IEnumerator SoundsTick()
     {
         //we're using an ienumerator so we can "wait" between loops.
         while (indexCount.Count != 0)
         {
             for (int x = 0; x < sourcesInScene.Count; x++)
             {
                 var i = Random.Range(0, indexCount.Count);
                 sourcesInScene[indexCount[i]].Play();
                 indexCount.RemoveAt(i);
 
                 yield return new WaitForSeconds(1f);
             }
 
             yield return null;
         }
     }
 }


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 kab_6182 · Jun 21, 2017 at 07:39 PM 0
Share

Thank you very much, This is great

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

72 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

Related Questions

How to make a loop yield for the length of audio,how to make a loop wait for audio lengh 2 Answers

Fade, Then Stop all other audiosources attached to gameobject? 1 Answer

small pause on audio loop unity 5 0 Answers

corotuine looping hang the editor. 1 Answer

Loop Coroutine For A Demo Mode 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