Search Unity

Changing Music

Discussion in 'Scripting' started by MonkeyKasai, Oct 25, 2014.

  1. MonkeyKasai

    MonkeyKasai

    Joined:
    Jan 4, 2014
    Posts:
    40
    Hey guys, I'm just trying to get the audioclip attached to my camera to change when changing scenes

    this is what i tried

    Code (JavaScript):
    1. var Music : AudioClip;
    2. var Player : GameObject;
    3. var CameraMusic : AudioSource;
    4.  
    5. function Start(){
    6.  
    7. Player = GameObject.FindWithTag("Player1");
    8. CameraMusic = GameObject.FindWithTag("MainCamera").GetComponent(AudioSource);
    9.  
    10. }
    11.  
    12. function Update(){
    13.  
    14.             if(Input.GetButtonDown("Run")){
    15.                 DontDestroyOnLoad (Player.gameObject);
    16.                 CameraMusic.AudioClip = Music;
    17.                 Application.LoadLevel("Cemetery");
    18.             }
    19.  
    20. }
    21.  

    Error I get is,
    MissingFieldException : UnityEngine.AudioSource.AudioClip
    Boo.Lang.Runtime.DynamicDispatching.PropetyDispatcherFactory.FindExtension (IEnumerable`1 candidates)
     
  2. MonkeyKasai

    MonkeyKasai

    Joined:
    Jan 4, 2014
    Posts:
    40
    Nevermind guys figured it out =)

    I changed
    CameraMusic.AudioClip = Music;
    to
    CameraMusic.clip = Music;
    Also had to add this to make it play
    CameraMusic.Play();
     
    Magiichan likes this.