Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Audio I have a problem. i am adding the audio but its not play

Discussion in 'Audio & Video' started by TaimoorMazhar, Jan 17, 2020.

  1. TaimoorMazhar

    TaimoorMazhar

    Joined:
    Jan 3, 2020
    Posts:
    4
    Hello Everyone,

    My issue is i have added the sounds in to my target script that attached with all my prefabs
    now when i am going to add a sound, e.g when my objects destroy then it play the sound and each prefab has a specific sound.

    and i have done all this but there is a issue sound is not playing i dont know what is the issue check my add code.
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Target : MonoBehaviour
    {
    private Rigidbody targetRb;
    private GameManager gameManager;
    private AudioSource targetAudio;
    public AudioClip prefabAudi;




    private float xBound = 4;
    private float minSpeed = 10; // spawn oper any ki speed hai
    private float maxSpeed = 20; // spawn ki oper jany tak ki max speed hai
    private float maxTouque = 10; // spawn k ghomny ki speed hai
    private float spawnPosX = 4; // spawn k left right spawn k any ki had k leye use kiya hai
    private float spawnPosY = 7; // ye spawn ki y position hai k kis pos sy uth k oper aye ga

    public int pointValue; // ye scoring value hai
    public ParticleSystem exposionParticle; // ye particle system jo destroy per instentiate hon gey
    // Start is called before the first frame update
    void Start()
    {
    targetRb = GetComponent<Rigidbody>();
    targetAudio = GetComponent<AudioSource>();
    gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();

    targetRb.AddForce (randomForce(), ForceMode.Impulse);
    targetRb.AddTorque(randomTorque(), randomTorque(), randomTorque());

    transform.position = randomPosition();
    }

    // Update is called once per frame
    void Update()
    {
    ConstrainPlayerPosition();

    }

    private void OnMouseDown()
    {


    if(gameManager.isGameActive)
    {
    targetAudio.PlayOneShot(prefabAudi, 2.0f);
    Destroy(gameObject);
    Instantiate(exposionParticle, transform.position, exposionParticle.transform.rotation);
    gameManager.updateScore(pointValue);
    }



    }

    private void OnTriggerEnter(Collider other)
    {

    Destroy(gameObject);
    if (!gameObject.CompareTag("Bad"))
    gameManager.GameOver();

    }

    Vector3 randomForce()
    {
    return Vector3.up * Random.Range(minSpeed, maxSpeed);
    }

    float randomTorque()
    {
    return Random.Range(-maxTouque, maxTouque);
    }

    Vector3 randomPosition()
    {
    return new Vector3(Random.Range(-spawnPosX, spawnPosX), -spawnPosY);
    }

    void ConstrainPlayerPosition()
    {
    if (transform.position.x < -xBound)
    {
    transform.position = new Vector3(xBound, transform.position.y, transform.position.z);

    }
    if (transform.position.x > xBound)
    {
    transform.position = new Vector3(xBound, transform.position.y,transform.position.z);
    }
    }
    }
     

    Attached Files:

  2. BubbleheadStudios

    BubbleheadStudios

    Joined:
    Oct 28, 2017
    Posts:
    6
    When you destroy and object with audio, you actually 'destroy' the audio along with it so it doesn't play.

    There are many ways of handling this, but maybe the easiest in this scenario is maybe to use the AudioSource.PlayClipAtPoint static function. This function creates a 'separate' AudioSource independent of the object you're destroying, and automatically destroys the audio when the clip has stopped playing.

    This may work (haven't tested it!)
    AudioSource.PlayClipAtPoint(prefabAudi, new Vector3(targetRb.transform.position));

    https://docs.unity3d.com/ScriptReference/AudioSource.PlayClipAtPoint.html
     
    TaimoorMazhar likes this.
  3. TaimoorMazhar

    TaimoorMazhar

    Joined:
    Jan 3, 2020
    Posts:
    4
    Yes, its worked Thanks Dude.
     
    BubbleheadStudios likes this.