• 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
Question by MedalHellWay · Mar 07, 2018 at 05:50 PM · c#sound

Start two sounds one after the other...

Hi all :)

After so much research, even here, I could not find the answer to my question. Basically I've a door to open. I inserted one sound for the opening and one sound for closing. Everything is ok. Now I want to insert a sound to unlock the door before opening it, but any method I use does not work. I found many tips on the web, using "WaitForSeconds" method, but for me dont work..

Do you have any advice?

Below a example of my script.. The WaitForSeconds is unchecked.I am a beginner with C#, so forgive me if it's not correct or clear

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Doors : MonoBehaviour 
 {
     public bool open = false;
     private bool hasOpenedCompletly;
 
     public float DoorOpenAngle = 90.0f;
     public float DoorCloseAngle = 0.0f;
     public float Smooth = 2.0f;
 
     public bool front = false;
     public bool back = false;
 
     [SerializeField] private AudioClip[] LockedDoorSound;
     [SerializeField] private  AudioClip[] OpenDoorSound;
     [SerializeField] private AudioClip[] UnlockedDoorSound;
     private AudioSource Audio; 
 
     public bool isLocking = false;
 
     void Start()
     {
         Audio = GetComponent<AudioSource> ();
     }
 
     public void ChangeDoorState()
     {
 
         if (isLocking != true) 
         {
             open = !open;
 
             if (Audio != null) 
             { 
                 PlayOpeningDoorSound();
             } 
         } 
         else 
         {
             PlayLockedDoorClosed();
         }    
     }
 
     private void PlayLockedDoorClosed()
     {
         int n = Random.Range(1, LockedDoorSound.Length);
         Audio.clip = LockedDoorSound[n];
         Audio.PlayOneShot(Audio.clip);
         LockedDoorSound[n] = LockedDoorSound[0];
         LockedDoorSound[0] = Audio.clip;
     }
 
     private void PlayOpeningDoorSound()
     {
         int r = Random.Range (1, OpenDoorSound.Length);
         Audio.clip = OpenDoorSound[r];
         Audio.PlayOneShot(Audio.clip);
         OpenDoorSound [r] = OpenDoorSound [0];
         OpenDoorSound [0] = Audio.clip;
     }
 
     private void PlayUnlockedDoorSound()
     {
         int u = Random.Range (1, UnlockedDoorSound.Length);
         Audio.clip = UnlockedDoorSound[u];
         Audio.PlayOneShot(Audio.clip);
         UnlockedDoorSound [u] = UnlockedDoorSound [0];
         UnlockedDoorSound [0] = Audio.clip;
     }
 
     /*public IEnumerable WaitForOpenDoors()
     {
         yield return new WaitForSeconds(2.2f);
         PlayUnlockedDoorSound ();
     }*/
 
     // Update is called once per frame
     void Update () 
     {
         if (open) 
         {
             Quaternion targetRotationOpen = Quaternion.Euler (-90, -DoorOpenAngle, 0);
                 transform.localRotation = Quaternion.Slerp (transform.localRotation, targetRotationOpen, Smooth * Time.deltaTime);
 
                 if (transform.localRotation == targetRotationOpen) 
                 {
                     hasOpenedCompletly = true;
                 }
         } 
 
         else
 
         {
             Quaternion targetRotationClose = Quaternion.Euler (-90, DoorCloseAngle, 0);
             transform.localRotation = Quaternion.Slerp (transform.localRotation, targetRotationClose, Smooth * Time.deltaTime);
             hasOpenedCompletly = false;
         }
             
     }
 }
 <code>



Comment

People who like this

0 Show 0
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

3 Replies

  • Sort: 
avatar image
Best Answer

Answer by MedalHellWay · Apr 17, 2018 at 03:32 PM

Ok, re-open this discussion because with a workhome I found a solution :)

There were many problems with the solutions mentioned above, so I decided to apply a script linked a collider box attached to the door for trigger solution. The script contains all the functions suitable to recreate the release of the bolt. It's work together with the script "Doors". This solution is more flexible:)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityStandardAssets.Characters.FirstPerson;
 
 public class DoorTriggerFront : MonoBehaviour 
 {
 
     [SerializeField] private AudioClip UnlockDoorSound;
     private AudioSource UnlockAudio;
 
     public Collider coll;
     [SerializeField] private GameObject Gobj;
 
     void Start()
     {
         UnlockAudio = GetComponent<AudioSource> ();
         coll = GetComponent <Collider>();
         coll.enabled = false;
     }
         
     public void OnTriggerEnter (Collider other) 
     {
         if (other.CompareTag ("Player")) 
             {
                 UnlockAudio.PlayOneShot(UnlockDoorSound);
                 StartCoroutine (Wfs());
             } 
     }
 
      IEnumerator Wfs()
     {
         Gobj.GetComponent<FirstPersonController> ().enabled = false;
         yield return new WaitForSeconds (UnlockDoorSound.length);
         Destroy (coll);
         Gobj.GetComponent<FirstPersonController> ().enabled = true;
     }
 
 
 }
Comment

People who like this

0 Show 0 · 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

Answer by bliu886 · Mar 07, 2018 at 07:01 PM

You can do something similar to this:

 void Update()
 {
     if (open)
     {
         StartCoroutine(OpenDoor());        
     }
 }
 
 private void IEnumerator OpenDoor()
 {
     PlayUnlockSound();
 
     // wait for 2 seconds
     yield return new WaitForSeconds(2.0f);
 
     Open();
     PlayOpenSound();
 
     yield return new WaitForSeconds(2.0f);
 
     Close();
     PlayCloseSound();
 }
Comment
MedalHellWay

People who like this

1 Show 6 · 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 MedalHellWay · Mar 07, 2018 at 09:53 PM 0
Share

Thanks @bliu886 for the answer :)

Dont work sorry. I explain everything better.

I've two script; "Interact" attach to gameobject "Player" and the script above attach to gameobject "Door". When I approach to the door with the player, I use a key to open it (another script called key). With the current situation, the door is opened immediately, but I would like wait a couple of seconds before opening the door using the desired sound. Now I've used "WaitForOpenDoors()" metod but without success. With your example, the door always opens immediately and the sound is looped.

My example below that does not work well because the door opens immediately with its sound, and when the door is completely open, start the sound that I want...

 StartCoroutine ("WaitForOpenDoors"); // in the script Interact
 
 [......]
 
 private IEnumerator WaitForOpenDoors() // in the script Door
      {
          yield return new WaitForSeconds(2.2f);
          PlayUnlockedDoorSound ();
      }

Which other solution can I adopt? Thanks again

avatar image bliu886 MedalHellWay · Mar 07, 2018 at 10:48 PM 1
Share

You need to reverse the play sound and yield to get the sound to play first and then do the door opening. I forgot that you needed to keep track of more than one state of the door other than open and closed, which you attempted to do with all of the boolean variables. A better way of doing this is with enums. Try this in your door script:

 // A list of the various states of the door.
 public enum DoorState
 {
     Locked,
     Unlocked,
     Closed,
     Opening,
     Open,
     Closing
 }
 
 public class Doors : Monobehaviour
 {
     // The current state of the door.
     public DoorState state = DoorState.Locked;
 
     // other variables
 
     void Update()
     {
         // person used the key and changed the door state from
         // locked to closed, so open the door.
         if (state == DoorState.Unlocked)
         {
             StartCoroutine(OpenDoor());
         }
         // Door has already been unlocked and should be opening
         else if (state == DoorState.Opening)
         {
             Quaternion targetRotationOpen = Quaternion.Euler(-90, -DoorOpenAngle, 0);
             transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotationOpen, Smooth * Time.deltaTime);
 
             // finished opening the door so change the state of the door from opening to open.
             if (transform.localRotation == targetRotationOpen) 
             {
                 state == DoorState.Open;
             }
         }
 
         // When you need to close the door, change the state of the door to closing to close the door.
         else if (state == DoorState.Closing)
         {
             Quaternion targetRotationClose = Quaternion.Euler (-90, DoorCloseAngle, 0);
             transform.localRotation = Quaternion.Slerp (transform.localRotation, targetRotationClose, Smooth * Time.deltaTime);
 
             // the door has finished closing, so changed the state of the door to closed.
             if (transform.localRotation == targetRotationOpen) 
             {
                 state == DoorState.Closed;
             }
         }
     }
 
     private IEnumerator OpenDoor()
     {
         // first play the unlock sound
         PlayUnlockSound();
 
         // wait 2.0 seconds for the sound to end
         yield return new WaitForSeconds(2.0f);
 
         // change the state of the door to open
         state = DoorState.Opening;
     }
 }


and in your Interact script do this:

 // Instead of this line
 StartCoroutine ("WaitForOpenDoors"); // in the script Interact
 
 // Use something like this to open the door:
 doorState = DoorState.Unlocked;
 
 


avatar image MedalHellWay bliu886 · Mar 08, 2018 at 09:04 PM 0
Share

I tried your solution but I had a lot of problems.

The most important is that the unlocking sound is repeated every time on all the doors and is a distracted sound. If I change the state of the door, I've undesirable results. I worked all day on the problem but without success :( As mentioned in the first post, I'm really a beginner in C# and I come from MaxScript, a much simpler and "intuitive" language. However, I don't give up! The strangest thing is that if I simplify the method "WaitForSeconds" (not using states), it does not work in any way! Why???? At the level of reasoning it is "simple"; I approach the door, interact with it, waiting time is as long as the sound (es. WaitForSeconds(sound.Lenght)) and then opens it...

Show more comments
avatar image

Answer by meat5000 · Mar 09, 2018 at 02:17 PM

For your coroutine issue Try starting your coroutine without " ", adding back in the ();

 StartCoroutine(SomeRoutine());

~

This has been answered, if I am not mistaken. If something works for you there give credit to the answerer in that thread.

~

In a nutshell you basically use the 'length' field of the audioclip to time your clip-replacement.

Comment
MedalHellWay

People who like this

1 Show 5 · 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 MedalHellWay · Mar 09, 2018 at 09:08 PM 0
Share

Thanks @meat5000 for your reply :)

The discussion you indicated was a starting point to solve my problem but I've more problem as mentioned above :( I tried in every way to insert "WaitForSeconds", but it does not work with my job. Surely I'm wrong to write the code, but now I'm stubborn to solve the problem.

If I write...

  private IEnumerator WaitForOpenDoors() // in the script Door
       {
           PlayUnlockedDoorSound ();
           yield return new WaitForSeconds (UnlockedDoorSound.Length);
       }

And ...

 StartCoroutine ("WaitForOpenDoors")

Dont work.. The logic would be to insert it in ChangeDoorState() (see first post) but nothing, dont work... I tried the solution of bliu886, but I've more problem as described. Using the "state" as suggested I've more conflicts with the script and it does not work as I expect. Surely the direction is right ... please help!! :D :D

avatar image meat5000 MedalHellWay · Mar 10, 2018 at 09:33 AM 0
Share

Tried without ""?

 StartCoroutine (WaitForOpenDoors());

~

I also notice in your original code that you use

 `IEnumerable WaitForOpenDoors()` instead of IEnumerator
avatar image MedalHellWay meat5000 · Mar 10, 2018 at 11:34 AM 0
Share

Yes, I changed "IEnumerable" to "IEnumerator" after having read the help and understood its operation better and followed by the suggestion of bliu886 ;)

StartCoroutine without "" dont work.

Today I try to rewrite the whole script with the suggestions, and I see what comes out... thanks for your help :)

Show more comments

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

460 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

in-Game Sound Settings: AudioMixer Problem with Singleton UI 0 Answers

2D jump sound not working. 1 Answer

Adding a CharacterController makes my player move around the map involuntarily 0 Answers

I'm trying to add sounds of jumping and nothing works. There are no compiling mistakes, files are put on their places and still no sound((( 0 Answers

GetSpectrumData Why last element in array allways is zero? 0 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