Search Unity

Audio How do i play audio in a script

Discussion in 'Audio & Video' started by masondrawz, Dec 29, 2019.

  1. masondrawz

    masondrawz

    Joined:
    Sep 26, 2019
    Posts:
    8
    Ok i have a script for picking S*** up that looks like this
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class pickupObject : MonoBehaviour
    6. {
    7.  
    8.     public Transform pickupTransform;
    9.  
    10.     void OnMouseDown()
    11.     {
    12.         GetComponent<Rigidbody>().useGravity = false;
    13.         GetComponent<Rigidbody>().freezeRotation = true;
    14.         GetComponent<MeshCollider>().enabled = false;
    15.         this.transform.position = pickupTransform.position;
    16.         this.transform.parent = GameObject.Find("objectPickupTransform").transform;
    17.     }
    18.  
    19.     void OnMouseUp()
    20.     {
    21.         this.transform.parent = null;
    22.         GetComponent<MeshCollider>().enabled = true;
    23.         GetComponent<Rigidbody>().useGravity = true;
    24.         GetComponent<Rigidbody>().freezeRotation = false;
    25.     }
    26.  
    27. }
    and basically on the void OnMouseUp bit i need it to play a sound but i have no F***in clue how to do that.

    The object its set up on has an audio source with the clip i want to play already shoehorned into it. I just need to know what i have cram into that void to get it to play.

    Bonus points to anyone who could get it to play another sound on the void OnMouseDown bit
     
  2. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    139
    Something like this, added using your script, should work...

    You can probably use GetComponent directly as you have been doing, or set it in the inspector. I've only set it in Start as an example.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class pickupObject : MonoBehaviour
    5. {
    6.     public Transform pickupTransform;
    7.     public AudioSource audioSource;
    8.     public AudioClip mouseDownAudioClip;
    9.     public AudioClip mouseUpAudioClip;
    10.  
    11.     void Start()
    12.     {
    13.         audioSource = gameObject.GetComponent<AudioSource>();
    14.     }
    15.  
    16.     void OnMouseDown()
    17.     {
    18.         GetComponent<Rigidbody>().useGravity = false;
    19.         GetComponent<Rigidbody>().freezeRotation = true;
    20.         GetComponent<MeshCollider>().enabled = false;
    21.         this.transform.position = pickupTransform.position;
    22.         this.transform.parent = GameObject.Find("objectPickupTransform").transform;
    23.         audioSource.PlayOneShot(mouseDownAudioClip, 1);
    24.     }
    25.     void OnMouseUp()
    26.     {
    27.         this.transform.parent = null;
    28.         GetComponent<MeshCollider>().enabled = true;
    29.         GetComponent<Rigidbody>().useGravity = true;
    30.         GetComponent<Rigidbody>().freezeRotation = false;
    31.         audioSource.PlayOneShot(mouseUpAudioClip, 1);
    32.     }
    33. }
    For other options for playing audio I wrote a whole article on the different methods and when to use each, which you can find here: https://johnleonardfrench.com/articles/how-to-play-audio-in-unity-with-examples/

    Hope that helps.
     
    masondrawz and marc_tanenbaum like this.