Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Play Audio when condition is met

Discussion in 'Audio & Video' started by SaamBell, Apr 27, 2015.

  1. SaamBell

    SaamBell

    Joined:
    Mar 28, 2014
    Posts:
    128
    Hey guys! I'm just getting used to Unity 5 and have run into a problem when making my game.

    As you can see in my code below, when the value, click.ore is more or equal to the cost, it will change the color of the icon, and well as play a sound effect. Whilst the color works, the sound is very distorted and keeps playing for a very long time, not working again when i reach my next value. To give you a clearer picture, here is a screenshot!

    Unityhelp.png

    Code (CSharp):
    1. void Update(){
    2.         itemInfo.text = itemName + " (" + count + ")" + "\nCost: " + cost + "\nPower: +" +clickPower;
    3.  
    4.         if(click.ore >= cost) {
    5.             GetComponent<Image>().color = affordable;
    6.             GetComponent<AudioSource>().PlayOneShot(ding);
    7.  
    8.  
    9.  
    10.  
    11.         } else {
    12.             GetComponent<Image>().color = standard;
    13.         }
    14.  
    15.  
    16.  
    17.  
    18.      
    19.     }
    could anyone help me so that the sound plays correctly when the value is met instead of getting this distorted effect. Thank you!
     
  2. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    because update is called a lot you code will execute a lot
    call play once from a button handler instead of continually from update
     
  3. SaamBell

    SaamBell

    Joined:
    Mar 28, 2014
    Posts:
    128
    And how would i go about doing that if you don't mind me asking :)?
     
  4. SaamBell

    SaamBell

    Joined:
    Mar 28, 2014
    Posts:
    128
    No matter! i fixed it by using a bool in my updateFuction to check if the ding sound had played or not! :)
     
  5. Good_Venson

    Good_Venson

    Joined:
    Oct 12, 2014
    Posts:
    22
    You would put a function like this on the game object, and in your button's event triggers you would add a reference to this function and pass in the amount of ore if you wanted to. For more on buttons see the information linked below. =P

    Code (CSharp):
    1. private int ore = 0;
    2. public int enoughOre = 10;
    3.  
    4. public Color affordable;
    5. public Color notAffordable;
    6.  
    7. private Image image;
    8. private AudioSource source;
    9.  
    10. public void AddOre(int amount = 1)
    11. {
    12.    ore += amount;
    13.  
    14.    if(!image){
    15.       if(GetComponent<Image>())
    16.          image = GetComponent<Image>();
    17.       else return;
    18.    }
    19.    if(!source){
    20.       if(GetComponent<AudioSource>())
    21.          source= GetComponent<AudioSource>();
    22.       else return;
    23.    }
    24.  
    25.    if(ore >= enoughOre){
    26.       GetComponent<Image>().color = affordable;
    27.       GetComponent<AudioSource>().PlayOneShot(ding);
    28.    }
    29.    else{
    30.       GetComponent<Image>().color = notAffordable;
    31.    }
    32. }
    http://docs.unity3d.com/Manual/script-Button.html
    http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button