Trying to get a key to play a sound when it is interacted with and disappears. Csharp
@Mavina I'll post the code below that I have already. The key was disappearing fine when it was in it's most basic state and then as soon as i tried to add in the audio side of it it stopped disappearing when I interacted with it and worst of all the audio dosnt even play. I think what might be wrong is that the key isnt destroying itself because it's waiting for the audio to play and finish but the audio isnt playing so it just sits there. Any help and insight would be greatly appreciated. Thank You !!
public class Key : MonoBehaviour { public new_door myDoor; public AudioClip pickupSound; public AudioSource audioSource;
public void Unlocknew_door()
{
audioSource = GetComponent<AudioSource> ();
}
public void UnlockDoor()
{
myDoor.isLocked = false;
audioSource.PlayOneShot(pickupSound);
StartCoroutine("WaitForSelfDestruct");
}
IEnumerator WaitForSelfDestruct()
{
yield return new WaitForSeconds(pickupSound.length);
Destroy(gameObject);
}
}
Answer by TBruce · Apr 14, 2016 at 04:23 AM
@OptimisticSoundGuy It is most likely that the GameObject is being destroyed before the sound can be played. I have restructured your code a little. Try this using UnityEngine; using System.Collections;
public class Key : MonoBehaviour
{
public new_door myDoor;
public AudioClip pickupSound;
public AudioSource audioSource;
void Start()
{
audioSource = GetComponent<AudioSource>();
if (audioSource == null)
{ // if AudioSource is missing
Debug.LogWarning("AudioSource component missing from this gameobject. Adding one.");
// let's just add the AudioSource component dynamically
audioSource = gameObject.AddComponent<AudioSource>();
}
}
public void UnlockDoor()
{
if (myDoor != null)
{
myDoor.isLocked = false;
}
StartCoroutine(WaitForSelfDestruct());
}
IEnumerator WaitForSelfDestruct()
{
audioSource.PlayOneShot(pickupSound);
yield return new WaitForSeconds(pickupSound.length);
StartCoroutine(DestroySelf());
}
IEnumerator DestroySelf()
{
yield return new WaitForSeconds(1.0f);
Destroy(gameObject); // this should really be done in another object
}
}
Answer by OptimisticSoundGuy · Apr 14, 2016 at 05:20 AM
Spot on man, I know all this stuff might seem simple to you but honestly iv got no idea how you figure this stuff out :L Thank you !!
@OptimisticSoundGuy Actually UnlockDoor() should be
public void UnlockDoor()
{
if (myDoor != null)
{
myDoor.isLocked = false;
}
if (pickupSound != null)
{
StartCoroutine(WaitForSelfDestruct());
}
else
{
StartCoroutine(DestroySelf());
}
}
When coding one should adhere to a set of coding standards. I know that much of this might seem redundant when reading through the code, but it is best to make sure that objects are valid before using them. This alone can help solve many problems. Also The console is your friend. $$anonymous$$ore than 95% of the time the console will tell you exactly where your error(s) are if you ave any.
Using the console for information while coding can also be very handy (take a look at the Start() function above, you have a public AudioSource property, if that block is entered than means an the Audio property was not set in the inspector two things happen here 1 - an AudioSource is added because we need one and this adds the benefit of not needing to verify if the AudioSource is valid later and 2 - a debug message is logged so that you can see the AudioSource property was never set in the inspector).
These are just a few things that I can point out that have allowed me to answer your questions. I myself have been coding for over 30 years and have been using Unity for over 3.5. This experience has also helped me. I wish you good luck in your ga$$anonymous$$g and any other coding endeavors.
Your answer
![](https://koobas.hobune.stream/wayback/20220612074811im_/https://answers.unity.com/themes/thub/images/avi.jpg)