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

check item from loop and play its audio clip

Discussion in 'Scripting' started by Deleted User, Aug 16, 2018.

  1. Deleted User

    Deleted User

    Guest

    hi,
    i have a for loop where it will choose an item and throw it in the scene .
    now i need to play an audio clip which refers to this item.
    can you help and say how to do it ?
    thanks
     
  2. Rioneer

    Rioneer

    Joined:
    Nov 27, 2013
    Posts:
    51
  3. Deleted User

    Deleted User

    Guest

    Last edited by a moderator: Aug 17, 2018
  4. Rioneer

    Rioneer

    Joined:
    Nov 27, 2013
    Posts:
    51
    You can assign your last thrown item to a field and when your function needs to run it can go like this :
    Code (CSharp):
    1. void FergusSpeaks() {
    2.     switch(lastThrownItem) {
    3.         case "Item 1" :
    4.             PlaySound("Item 1");
    5.             break;
    6.         case "Item 2" :
    7.             PlaySound("Item 2");
    8.             break;
    9.     }
    10. }
    However what I usually do in cases like these is to match the sound files' names to my variables' names so I can easily do this instead of dealing with a switch statement :
    Code (CSharp):
    1. PlaySound(lastThrownItem.toString());