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

"Insert a semicolon at the end." but I can't see one missing?

Discussion in 'Scripting' started by rebecca_peace, Jan 6, 2016.

  1. rebecca_peace

    rebecca_peace

    Joined:
    Jan 6, 2016
    Posts:
    10
    Hi,

    I'm very new to coding and am getting very frustrated. I haven't managed to get a single script to work over the last few days :(

    All I want is a random array of 5 sounds to play one at a time at random time intervals (they are bird sounds so want them random to produce a more realistic environment) but have only got this far:

    #pragma strict

    var playlist : AudioClip[5];

    function PlayRandom(){
    if (audio.isPlaying) return;
    audio.clip = sounds[Random.Range(0,sounds.length)];
    audio.Play();
    }

    I keep getting the error:

    "Assets/MyScripts/Birdarray.js(3,25): UCE0001: ';' expected. Insert a semicolon at the end."

    but I can't see one missing anywhere. Can anyone help? I'll be eternally grateful - am going mad just staring at the code trying to understand it!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Your array declaration is malformed, which is confusing the compiler. Remove the '5' from the brackets.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  4. rebecca_peace

    rebecca_peace

    Joined:
    Jan 6, 2016
    Posts:
    10
    Sorry I didn't know how to do that - thanks LeftyRighty!

    Here is the code again in the tags:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var playlist : AudioClip[5];
    4.  
    5. function PlayRandom(){
    6. if (audio.isPlaying) return;
    7. audio.clip = sounds[Random.Range(0,sounds.length)];
    8. audio.Play();
    9. }
     
  5. Gnatty

    Gnatty

    Joined:
    May 17, 2012
    Posts:
    77
    Yes so instead do this
    Code (JavaScript):
    1. var playlist : AudioClip[] = new AudioClip[5];
     
  6. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Code (CSharp):
    1. #pragma strict
    2. var playlist : AudioClip[] = new AudioClip[5];
    Incorrect variable array decleration.
     
  7. rebecca_peace

    rebecca_peace

    Joined:
    Jan 6, 2016
    Posts:
    10
    Thanks StarManta but removing the 5 from the brackets brings up loads more error messages (though it does get rid of the semicolon one!):

    Assets/MyScripts/Birdarray.js(6,10): BCE0144: 'UnityEngine.Component.audio' is obsolete. Property audio has been deprecated. Use GetComponent<AudioSource>() instead. (UnityUpgradable)
    Assets/MyScripts/Birdarray.js(6,16): BCE0019: 'isPlaying' is not a member of 'UnityEngine.Component'.
    Assets/MyScripts/Birdarray.js(7,12): BCE0019: 'clip' is not a member of 'UnityEngine.Component'.
    Assets/MyScripts/Birdarray.js(7,19): BCE0005: Unknown identifier: 'sounds'.
    etc...

    Any more ideas?
     
  8. rebecca_peace

    rebecca_peace

    Joined:
    Jan 6, 2016
    Posts:
    10
    Thank you but that brings up the same error messages which appear when I remove the 5 from the brackets (See reply to StarManta). The script is just attached to an empty object which I intend to play the sounds from - I don't know if that is causing any problems?
     
  9. Gnatty

    Gnatty

    Joined:
    May 17, 2012
    Posts:
    77
    Okay try this.
    Using the new API I have used GetComponent to get the audio source component.
    I've replaced "sounds" with "playList", I assume this is correct, but I'm not sure.
    Code (CSharp):
    1. #pragma strict
    2. var playlist : AudioClip[];
    3.  
    4. function PlayRandom(){
    5.     var audioSource : AudioSource = GetComponent.<AudioSource>();
    6.     playlist = new AudioClip[5];
    7.  
    8.     if (audioSource.isPlaying) return;
    9.     audioSource.clip = playlist[Random.Range(0, playlist.length)];
    10.     audioSource.Play();
    11. }
    12.  
    Maybe you don't want to initialise playList here, but in the Inspector instead, in which case remove the initialisation which is this line ...
    Code (CSharp):
    1. playlist = new AudioClip[5];
    2.  
     
  10. rebecca_peace

    rebecca_peace

    Joined:
    Jan 6, 2016
    Posts:
    10
    Thank you! No errors yippee! How do I add the sounds in now? It just says "Playlist - Size: 0" in the inspector. I can change the number 0 but it goes straight back when I click off.
     
  11. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    this was a change when unity5 was released, the quick accessors like "audio" were removed. As the error says, you now need to explicit GetComponent call for them.

    usually it's easiest to just declare a class variable and do that in awake/start
    Code (csharp):
    1.  
    2. AudioSource audio;
    3.  
    4. function Start()
    5. {
    6. audio = GetComponent.<AudioSource>();
    7. }
    8.  
    once that is sorted out it should fix the other errors complaining about not being a member of component since it'll know you're using an audiosource not a generic component
     
  12. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    drag the audioclips onto the inspector and it'll add them to the array
     
  13. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Just a note: "More error messages" does not equal "more problems". In this case, those errors were always there, but the syntax error was keeping the compiler from understanding the code enough to know that those problems were there.

    Imagine the compiler is a mechanic. You take your car to the mechanic, he pops the hood, and tells you, "Well, all I can tell you is that there's a giant tiger under your hood and I'm scared to go near it." When you remove the tiger, he takes a look and says "Well, your oil needs changed, your battery is dead, your spark plugs are missing..." You obviously wouldn't put the tiger back to get your number of problems down to one instead of three. :)
     
    flashframe and Munchy2007 like this.
  14. rebecca_peace

    rebecca_peace

    Joined:
    Jan 6, 2016
    Posts:
    10
    Thank you - that worked too! You're all so lovely and helpful!
    I have one last problem (and I'm very sorry for all the questions and being so rubbish at this) - I can't hear my sounds in the game? I have other sounds that work and an audio listener and all that.. But can't hear these ones?
     
  15. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Do you ever actually call PlayRandom?
     
  16. rebecca_peace

    rebecca_peace

    Joined:
    Jan 6, 2016
    Posts:
    10
    This is the script I'm using as created by Gnatty

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var playlist : AudioClip[];
    4.  
    5. function PlayRandom(){
    6.     var audioSource : AudioSource = GetComponent.<AudioSource>();
    7.     if (audioSource.isPlaying) return;
    8.     audioSource.clip = playlist[Random.Range(0, playlist.length)];
    9.     audioSource.Play();
    10. }
    Does that call PlayRandom next to the function? Or does it need stating again?
     
  17. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    So, no, it is not being called. I don't know under what circumstances you want this function to run (on start? when a collision happens? etc), so with a little more information I can help you with that. But at some point, you need to call PlayRandom();
     
  18. rebecca_peace

    rebecca_peace

    Joined:
    Jan 6, 2016
    Posts:
    10
    Okay thank you.

    The situation is I have a panel in a canvas with a story on that you click through and read via a button. When that all disappears that's when I'd like the bird sounds to start. And then I'd like the 5 clips to play at random at random times (though if playing at random times is not possible then a waitforseconds will suffice), just 2D is fine. The script and sounds are currently on an empty object in my game.

    Does that make sense? Thank you so much!
     
  19. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    If I understand you correctly, you're going for basically some ambient background sounds... these birds will continue to chirp at random times, as long as the scene is open (or until something else happens and you decide to cancel it).

    That's easy enough; here is how I would code it. (Sorry, I don't speak UnityScript, so this is in C#.)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. [RequireComponent(typeof(AudioSource))]
    5.  
    6. public class RandomSounds : MonoBehaviour {
    7.     [Tooltip("List of sounds to play")]
    8.     public AudioClip[] sounds;
    9.    
    10.     [Tooltip("Minimum interval (in seconds) between sounds.")]
    11.     public float minInterval = 1;
    12.    
    13.     [Tooltip("Maximum interval (in seconds) between sounds.")]
    14.     public float maxInterval = 10;
    15.    
    16.     [Tooltip("Whether we should play sounds at all.")]
    17.     public bool playing = true;
    18.    
    19.     float nextPlayTime = 0;
    20.    
    21.     void Start() {
    22.         nextPlayTime = Time.time + Random.Range(minInterval, maxInterval);
    23.     }
    24.    
    25.     void Update() {
    26.         if (!playing || Time.time < nextPlayTime) return;
    27.         AudioSource audio = GetComponent<AudioSource>();
    28.         audio.clip = sounds[Random.Range(0, sounds.Length)];
    29.         audio.Play();
    30.         nextPlayTime = Time.time + audio.clip.length + Random.Range(minInterval, maxInterval);
    31.     }
    32.  
    33. }
    The code should be pretty easy to understand, but please let me know if anything isn't clear.

    So you can attach this to any object in your scene, load up the list of sounds you want it to play, and it'll just do its thing — nothing else required. But if you want, you can set its "playing" public property to start or stop it. (You can even set this from a UnityEvent, for example on a UI button — no code required!)
     
    rebecca_peace likes this.
  20. rebecca_peace

    rebecca_peace

    Joined:
    Jan 6, 2016
    Posts:
    10
    Thank you so much that looks amazing and I have pasted it into my game but I can't add it to an object as a pop up box comes up saying:

    "Can't add script component 'Birds' because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name match."

    What does this mean?
     
  21. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    It means that you pasted my code (which defines a class called RandomSounds) into a script called Birds.cs. The name of the script and the name of the MonoBehaviour inside the script must match.

    I recommend renaming your file to RandomSounds, because that's more descriptive of what this actually does — it just plays random sounds. It knows nothing about birds. You could use it for wind chimes, dogs barking, random bleeps and bloops in a spaceship cockpit, or whatever.
     
    rebecca_peace likes this.
  22. rebecca_peace

    rebecca_peace

    Joined:
    Jan 6, 2016
    Posts:
    10
    Thank you that worked brilliantly! I shall be mindful of my script names in future! Yay! Thank you so much :)
     
    JoeStrout likes this.