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 nathanvj · Jun 03, 2016 at 06:17 AM · c#scripting problemaudiosourcevariablesaudioclip

AudioClip stuttering while loop is OFF and not using function Update?

Hi.

I have a problem. I'm making a game where there are pixel art 'BOMBS' as enemies. My player dies when he hit the bomb, and exploding animation plays, and an AudioClip should play once. However, the AudioClips starts repeating itself an infinite amount of times, resulting in a sound that sounds like my computer is stuck.

Here is the code attached to the bomb.

 using UnityEngine;
 using System.Collections;
 
 public class bombScript : MonoBehaviour {
 
     Animator animator;
     SpriteRenderer bomb;
     static public bool hitbomb = false;
     GameObject dragon;
     AudioSource bombAudio;
     public AudioClip bombClip;
 
     // Use this for initialization
     void Start () {
         animator = gameObject.GetComponentInChildren<Animator> ();
         dragon = GameObject.FindGameObjectWithTag ("Player");
         bombAudio = GameObject.Find ("SoundManager/bombAudio").GetComponent<AudioSource> ();
     }
 
     void OnTriggerEnter2D(Collider2D player) {
 
         if(dragon.GetComponent<beta_DragonMovement>().Godmode == true)
             return;
 
         if (player.tag == "Player") {
             bombAudio.PlayOneShot (bombClip);
             bomb = gameObject.GetComponent<SpriteRenderer> ();
             bomb.enabled = false;
             hitbomb = true;
             animator.SetTrigger ("death");
         }
     
     }
         
 }
 

I also tried the following script:
It resulted in the same problem.
Wah wah.

 using UnityEngine;
 using System.Collections;
 
 public class bombScript : MonoBehaviour {
 
     Animator animator;
     SpriteRenderer bomb;
     static public bool hitbomb = false;
     GameObject dragon;
     AudioSource bombAudio;
 
     // Use this for initialization
     void Start () {
         animator = gameObject.GetComponentInChildren<Animator> ();
         dragon = GameObject.FindGameObjectWithTag ("Player");
         bombAudio = GameObject.Find ("SoundManager/bombAudio").GetComponent<AudioSource> ();
     }
 
     void OnTriggerEnter2D(Collider2D player) {
 
         if(dragon.GetComponent<beta_DragonMovement>().Godmode == true)
             return;
 
         if (player.tag == "Player") {
             bombAudio.Play ();
 // the bombClip is pre-set in the inspector.
             bomb = gameObject.GetComponent<SpriteRenderer> ();
             bomb.enabled = false;
             hitbomb = true;
             animator.SetTrigger ("death");
         }
     
     }
         
 }
 
Comment
Add comment · Show 1
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 nathanvj · Jun 02, 2016 at 11:51 PM 0
Share

Oh, and the AudioSource loop setting is unchecked!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by $$anonymous$$ · Jun 03, 2016 at 07:13 AM

This calls for some debugging. I would guess that your player hits multiple bombs that all start playing your clip. Add a Debug.log("some log string"); and check how often it is called.

You could use your static hitbomb boolean to only play the audio when hitbomb is false so that it only plays for one bomb.

Happy debugging!

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 nathanvj · Jun 03, 2016 at 10:00 AM 0
Share

Hey, thanks for your response.

I already tried the Debug.Log("playing bombAudio"); but it is just called once.

I have 3 three bombs, that all have the bombScript attached to them. The bombs are placed at random y-positions and pre-set x-positions.

The bombs are being moved to a new position when the player does not collide with them.

I tried to use a boolean, but it doesn't work either. However, if I find something that works, it's still very strange that this is happening, right?

Thanks for your time anyway!

avatar image nathanvj · Jun 03, 2016 at 10:03 AM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class bombScript : $$anonymous$$onoBehaviour {
 
     Animator animator;
     SpriteRenderer bomb;
     static public bool hitbomb = false;
     GameObject dragon;
     AudioSource bombAudio;
 
     bool playedOnce = false;
 
     // Use this for initialization
     void Start () {
         animator = gameObject.GetComponentInChildren<Animator> ();
         dragon = GameObject.FindGameObjectWithTag ("Player");
         bombAudio = GameObject.Find ("Sound$$anonymous$$anager/bombAudio").GetComponent<AudioSource> ();
     }
 
     void OnTriggerEnter2D(Collider2D player) {
 
         if(dragon.GetComponent<beta_Dragon$$anonymous$$ovement>().Godmode == true)
             return;
 
         if (player.tag == "Player") {
             if (playedOnce = false) {
                 bombAudio.Play ();
             }
             bomb = gameObject.GetComponent<SpriteRenderer> ();
             bomb.enabled = false;
             hitbomb = true;
             animator.SetTrigger ("death");
         }
 
         playedOnce = true;
     
     }
         
 }
 

I also tried this. Here I am using a boolean to check if the audio has played already, so that after it played once, it will not play again. However, this doesn't work either... Really hope to find a solution because it's messing up my whole game.

avatar image $$anonymous$$ nathanvj · Jun 06, 2016 at 12:02 PM 0
Share

Using the boolean playedOnce like you did in your code won't work. Each bomb has an instance of your code and each has it's own playedOnce boolean. If you want a boolean which is the same for each bomb you have to make a static bool PlayedOnce.

I can't help you with the exact solution but I hope I can send you in the right direction. If you have a solution or cause I'm interested!

avatar image nathanvj $$anonymous$$ · Jun 06, 2016 at 12:18 PM 0
Share

I didn't find the cause, but I fixed it by just rebuilding everything related to the sound. (only took an hour or two). Ins$$anonymous$$d of having a different AudioSource for every GameObject (bomb, gem, coin, etc) I made one AudioSource called 'SoundEffects'.

Here is my final code if you are interested.

 using UnityEngine;
 using System.Collections;
 
 public class GemScript : $$anonymous$$onoBehaviour {
 
     public bool blueGem = false;
     public bool greenGem = false;
     public bool redGem = false;
     public bool diamondGem = false;
 
     AudioSource soundfx;
     public AudioClip gemClip;
 
     void Start () {
         soundfx = GameObject.Find ("Sound$$anonymous$$anager/SoundEffects").GetComponent<AudioSource> ();
     }
 
     void OnTriggerEnter2D(Collider2D collider){
         if (collider.tag == "Player") {
             if (blueGem) {
                 Score.AddBlueGems ();
                 soundfx.pitch = 1.0f;
                 soundfx.PlayOneShot (gemClip);
             } else if (greenGem) {
                 Score.AddGreenGems ();
                 soundfx.pitch = 1.2f;
                 soundfx.PlayOneShot (gemClip);
             } else if (redGem) {
                 Score.AddRedGems ();
                 soundfx.pitch = 1.4f;
                 soundfx.PlayOneShot (gemClip);
             } else if (diamondGem) {
                 Score.AddDiamondGems ();
                 soundfx.pitch = 1.7f;
                 soundfx.PlayOneShot (gemClip);
             }
                 
                    // changing back to same sound for every gem (ignore):
             // soundfx.PlayOneShot (gemClip);
             GetComponent<SpriteRenderer> ().enabled = false;
 
         }
     }
 }
 

Thanks for your help and interest by the way! :-)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Audio Clip Playing every frame 2 Answers

Multiple gapless sound loops from one AudioSource (C#) 2 Answers

[SOLVED] audio background music making buzzing sound 2 Answers

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

2 animations keep switching at a rapid pace 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