Search Unity

Help with playing sounds properly

Discussion in 'Scripting' started by _maximus_, Jul 30, 2014.

  1. _maximus_

    _maximus_

    Joined:
    Jul 22, 2014
    Posts:
    2
    Hi all and thank you for reading this,

    I am quite new to Unity, previously I have been an active hobby Flash / Web developer for 5 years, so I know Actionscript quite well and are also familiar with JavaScript, HTML, CSS, PHP, which, mainly actionscript / flash and javascript seem to help a lot, but there are a few unclear things.

    I managed to play a sound by code, but when I try to know the current position in seconds it always returns 0. I would really appreciate any help! Thanks!

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. // VARS
    4.  
    5. var mainMusic : AudioClip; // the audioclip I play
    6.  
    7. var mainStream : AudioSource;
    8. mainStream = GameObject.Find("Plane").AddComponent("AudioSource"); // plane is an existing gameobject on the scene
    9.  
    10. // CUSTOM FUNCTIONS
    11.  
    12. function playMusicFromStart() { // starting to play the music
    13.  
    14.     mainStream.PlayClipAtPoint(mainMusic , new Vector3(0,0,0) );
    15.  
    16. }
    17.  
    18. // TRIGGER FUNCTIONS
    19.  
    20. function Start () {
    21.  
    22.     playMusicFromStart();
    23.  
    24. }
    25.  
    26. function Update () {
    27.  
    28.     Debug.Log(mainStream.time);
    29. }
    I don't get any error messages, it runs well, plays the sound and everything, but it always returns 0 as the song's position for some reason :S This script is attached to the main camera.
     
  2. Josh-Naylor

    Josh-Naylor

    Administrator

    Joined:
    Jul 1, 2014
    Posts:
    216