Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

AudioSource problem

Discussion in 'Scripting' started by LPocay, Jul 1, 2015.

  1. LPocay

    LPocay

    Joined:
    Jun 21, 2015
    Posts:
    9
    Grettings! i have a issue here im trying to put 2 audioclips in 1 audio sources with an array but im having this error:
    Assets/Scripts/FlashLight.cs(12,17): error CS0029: Cannot implicitly convert type `UnityEngine.AudioSource' to `UnityEngine.AudioSource[]'

    Here is my code:
    Code (CSharp):
    1. public class FlashLight : MonoBehaviour {
    2.     public Light lightSource;
    3.     private bool lighton=true;
    4.     public AudioSource[] sounds;
    5.     AudioSource flashOn;
    6.     AudioSource flashOff;
    7.     void Start () {
    8.         lightSource = GetComponent<Light> ();
    9.         sounds = GetComponent<AudioSource> ();
    10.         flashOn = sounds [1];
    11.         flashOff = sounds [0];
    12.     }
    13.    
    14.  
    15.     void Update () {
    16.  
    17.         if (Input.GetKeyUp (KeyCode.F)) {
    18.             toggleFlashlight ();
    19.             if(lighton){
    20.                 lighton=false;
    21.             }else{
    22.                 lighton=true;
    23.             }
    24.         }
    25.     }
    26.     void toggleFlashlight(){
    27.         if (lighton) {
    28.             lightSource.enabled = false;
    29.             flashOff.Play();
    30.         } else {
    31.             lightSource.enabled = true;
    32.             flashOn.Play();
    33.         }
    34.     }
    35. }
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    You are trying to assign the audioSource component to the array and not an index of an array.
     
  3. LPocay

    LPocay

    Joined:
    Jun 21, 2015
    Posts:
    9
    And what should i do? sorry im learnig
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    sounds is the audiosource, its singular, you cannot then use it as an array.

    you will need to create a collection to hold the clips you want to switch through and set the audiosource's "currentclip" to the one you want when you want to play it.


    http://docs.unity3d.com/ScriptReference/AudioSource-clip.html
     
    LPocay likes this.
  5. LPocay

    LPocay

    Joined:
    Jun 21, 2015
    Posts:
    9
    Thanks a lot! i solved my problem with your help! thanks again!