Search Unity

Assets/Collisions.cs(5,34): error CS1041: Identifier expected

Discussion in 'Scripting' started by ThuverX, May 25, 2015.

  1. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    I got this error and I don't know how to fix it!
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Collisions : MonoBehaviour {
    5.     @script RequireComponent(AudioSource);
    6.         public var AudioClip;
    7.  
    8.     IEnumerator OnCollisionEnter() {
    9.         AudioSource sound = gameObject.GetComponent<AudioSource>();
    10.         sound.Play();
    11.         yield return new WaitForSeconds (2);
    12.         Destroy (gameObject);
    13.     }
    14. }
    15.  
    Also can anybody help me how the sounds work? I'm new to game development.
    Thanks if helped!
     
  2. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    455
    If you just want to delete the object after 2 seconds, you don't need to create an IEnumerator because Destroy can handle the wait on it's own. Other than that, you were missing some necessary declarations in the OnCollisionEnter function definition. Now it's setup to know to detect Collision information and that the object it's colliding with will be assigned to a variable called "other"...

    Code (CSharp):
    1. void OnCollisionEnter(Collision other) {
    2.             AudioSource sound = gameObject.GetComponent<AudioSource>();
    3.             sound.Play();
    4.             Destroy (gameObject, 2.0f);
    5.         }
     
    sluice likes this.
  3. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35

    Thank you, But I still have the error, Did i do anything wrong?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Collisions : MonoBehaviour {
    5.     @script RequireComponent(AudioSource);
    6.         public var AudioClip;
    7.  
    8.     void OnCollisionEnter(Collision other) {
    9.         AudioSource sound = gameObject.GetComponent<AudioSource>();
    10.         sound.Play();
    11.         Destroy (gameObject, 2.0f);
    12.     }
    13. }
    14.  
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Line 5 looks like JavaScript syntax. Delete the line and the error will probably go away.
     
    krougeau likes this.
  5. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    455
    Also would help to know what the error was ;)
     
  6. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Instead of this
    Code (csharp):
    1.  
    2. public class Collisions : MonoBehaviour {
    3.     @script RequireComponent(AudioSource);
    4.         public var AudioClip;
    5. ...
    6.  
    do this
    Code (csharp):
    1.  
    2. [RequireComponent(typeof(AudioSource))]
    3. public class Collisions : MonoBehaviour {
    4.         public var AudioClip;
    5. ...
    6.  
    As @BoredMormon says, @script RequireComponent is for javascript, in C# you would use [RequireComponent(...)].
     
    Kiwasi, ThuverX and krougeau like this.
  7. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    I changed it, The error is gone but now i have a new one;
    Assets/Collisions.cs(6,16): error CS0825: The contextual keyword `var' may only appear within a local variable declaration

    Code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(AudioSource))]
    5. public class Collisions : MonoBehaviour {
    6.     public var AudioClip;
    7.  
    8.     void OnCollisionEnter(Collision other) {
    9.         AudioSource sound = gameObject.GetComponent<AudioSource>();
    10.         sound.Play();
    11.         Destroy (gameObject, 2.0f);
    12.     }
    13. }
    14.  
     
  8. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    Look at the name of the post.
     
  9. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    455
    Sorry, it's been a dreadfully long night and turning into a long day already haha. As for the var error, try this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [RequireComponent(typeof(AudioSource))]
    4. public class Collisions : MonoBehaviour {
    5.     public AudioClip snd;
    6.     void OnCollisionEnter(Collision other) {
    7.         AudioSource sound = gameObject.GetComponent<AudioSource>();
    8.         sound.Play(snd);
    9.         Destroy (gameObject, 2.0f);
    10.     }
    11. }
     
    Kiwasi likes this.
  10. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    Thank you for your help! But I still got errors?:(
    I changed to your code, then I got these

    Assets/Collsions.cs(10,23): error CS1502: The best overloaded method match for `UnityEngine.AudioSource.Play(ulong)' has some invalid arguments

    Assets/Collsions.cs(10,23): error CS1503: Argument `#1' cannot convert `UnityEngine.AudioClip' expression to type `ulong'
     
  11. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    455
    My bad... Should have gone to bed hours ago, haha. One sec, I'll getcha working...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [RequireComponent(typeof(AudioSource))]
    4. public class Collisions : MonoBehaviour {
    5.     public AudioClip snd;
    6.     void OnCollisionEnter(Collision other) {
    7.         AudioSource sound = gameObject.GetComponent<AudioSource>();
    8.         sound.clip = snd;
    9.         sound.Play();
    10.         Destroy (gameObject, 2.0f);
    11.     }
    12. }
    13.  
     
  12. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    No errors now, but it just doesn't work now :confused:

    (Also I hope I'm not bothering you, :p)
     
  13. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    455
    LOL! No worries at all ;) I just went through and tested it on my side and got it working... First let's knock out the obvious: you did drag and drop a sound clip into the "snd" slot of whatever object you have this script on, right?

    If that's not the problem, try changing
    Code (CSharp):
    1. AudioSource sound = gameObject.GetComponent<AudioSource>();
    to
    Code (CSharp):
    1. AudioSource sound = GetComponent<AudioSource>();
     
  14. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    I have this:

    But it still does not work, even when I change the code.
     
  15. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    455
    OK, there's the problem. We're not assigning the Audio Clip directly to the Audio Source (although you could, so long as that's the only sound you want to play), we're assigning it through the script we've just written, which should now have a slot called "Snd" -- that's where you want to put your sound file, in that slot.
     
  16. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    Thanks a lot! At first it did not work but when I restarted unity it worked!
     
    Last edited: May 25, 2015
    krougeau likes this.
  17. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    455
    Awesome! Glad it worked out ;) Sorry I was so scatterbrained, haha. Have fun!