Playing Music Problem.

Hello!

So my script plays music after 3 seconds after you click play, but when the scene changes, and you come back to the scene wit the music, it starts right away, because the game has been playing for longer then 3 seconds. But I am getting an error saying that “void is not a valid macro.” Someone on the last thread said it was because I had capitals, but I did not. Here is the script:

var song : AudioSource;
   
 function Update () {
       
     if (!song.isPlaying && Time.time >= 3) {
         song.Play();
     }
 }
 
 void Start()
 {
     Invoke("PlayMusic" , 3);
 }
      
 void PlayMusic()
 {
     song.Play();
 }

Hopefully some can help me out, I have been delaying the game because of this.

Use a couple more variables.

var song : AudioSource;
var timeSinceClickedPlay : float;
var timeUntilStartSong : float = 3f;
    
  function Update () {
        
      if (!song.isPlaying && timeSinceClickedPlay >= timeUntilStartSong) {
          song.Play();
      }
      else {
          timeSinceClickedPlay += Time.deltaTime;
      }
  }
  
  void Start()
  {
      Invoke("PlayMusic" , 3);
  }
       
  void PlayMusic()
  {
      song.Play();
  }

Your script contains a mixture of C# syntax:

void Start ()

And Javascript syntax:

function Update ()

You need to fix this before the script will compile.