Search Unity

[Help] AudioSource

Discussion in 'Scripting' started by ChronoMoon, Aug 21, 2017.

  1. ChronoMoon

    ChronoMoon

    Joined:
    Jul 28, 2017
    Posts:
    5
    Hi, sorry for what seems to be probably an easy question - but i'm new to all of this.
    I'm following a tutorial, but also trying to put my own spin on things by including sounds etc, that the tutorial does not cover.

    in the below code, shotFired is an AudioSource that plays a soundfile, in my case it should be playing on awake each time the bullet is fired.

    So i've tried to enable it during this process... the problem is it only plays on the very first bullet the turret fires. I've tried setting it to disabled at the end of the function, and ive also tried to set shotFired to disabled during the update function - but due to the nature of the update function the soundfile is cut off and hence not what im looking for....

    any help would be appreciated!

    Code (csharp):
    1. void Shoot()
    2.     {
    3.         shotFired.enabled = true;
    4.         Debug.Log("Turret Shot");
    5.         GameObject bulletGO = (GameObject)Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); //Reference the bullet that is currently instantiated
    6.  
    7.         Bullet bullet = bulletGO.GetComponent<Bullet>(); //Grab the bullet script off of the game object
    8.  
    9.         if(bullet != null) { bullet.Seek(target); }
    10.     }
     
  2. ChronoMoon

    ChronoMoon

    Joined:
    Jul 28, 2017
    Posts:
    5
    It seems i've been going about this wrong.

    I turned off play on awake and the audio is enabled from the beginning, and just have shotFired.Play(); which works perfectly.

    Seems I've solved my own issue.