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
  4. Dismiss Notice

New to this missing ","?

Discussion in 'Scripting' started by batman313v, Mar 29, 2019.

  1. batman313v

    batman313v

    Joined:
    Mar 29, 2019
    Posts:
    3
    This is my code. It's supposed to load a new scene when the player touches a cube but it has these errors.
    "
    Assets\Level Finish.cs(4,19): error CS1002: ; expected
    Assets\Level Finish.cs(4,19): error CS0116: A namespace cannot directly contain members such as fields or methods
    Assets\Level Finish.cs(4,31): error CS1022: Type or namespace definition, or end-of-file expected
    Assets\Level Finish.cs(6,37): error CS1001: Identifier expected
    Assets\Level Finish.cs(6,37): error CS1003: Syntax error, ',' expected
    Assets\Level Finish.cs(6,39): error CS1003: Syntax error, ',' expected
    Assets\Level Finish.cs(6,47): error CS1001: Identifier expected
    "
    This is probably very obvious but I just started with C# so... Thanks

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine SceneManager;
    5.  
    6. function OnTriggerEnter(CollabProxy : Collider)
    7. {
    8.     if(CollabProxy.tag == "Player")
    9.     {
    10.         Application.LoadLevel(Application.loadedLevel + 1);
    11.     }
    12. }
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    You lost that little dot inbetween. Here it is:
    Code (CSharp):
    1. using UnityEngine.SceneManager;
    Also, parameter declaration order is reversed and : is not needed

    Code (CSharp):
    1. function OnTriggerEnter(Collider CollabProxy)
    I recommend you Jeffrey Richter book on C#/.NET. My personal favorite.
     
  3. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You are also missing a class declaration. It looks like you have tried to copy code from Unityscript and C#.
     
    Bunny83 likes this.
  4. batman313v

    batman313v

    Joined:
    Mar 29, 2019
    Posts:
    3
    Thank you!!
     
  5. batman313v

    batman313v

    Joined:
    Mar 29, 2019
    Posts:
    3
    You're probably right. How do I fix it? I now have these
    "
    Assets\LevelFinish.cs(6,10): error CS0116: A namespace cannot directly contain members such as fields or methods
    Assets\LevelFinish.cs(6,1): error CS0246: The type or namespace name 'function' could not be found (are you missing a using directive or an assembly reference?)
    "
     
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,360
    This line:
    Code (csharp):
    1. function OnTriggerEnter(CollabProxy : Collider)
    Is actually not C#. That's the main problem you have. It looks like you may have copy and pasted code from a UnityScript listing. UnityScript is an old programming language that Unity no longer supports. Fortunately, it's similar to C# so it's not difficult to convert.

    That line above should be:
    Code (csharp):
    1. void OnTriggerEnter (Collider CollabProxy)
    Also, C# methods and variables always need to be inside of a class definition. To see an example class definition, try creating a "new C# script" in the project window. Unity will create a default C# script with a class definition inside. When you open it, you'll see where it says something like
    public class ScriptName:MonoBehaviour
    ScriptName is where the class name should go, so replace that with the name of the class. You basically need to create a similar class definition for your own code.

    Notice that there is "{" after that line and another "}" at the end of the file. In C#, these symbols are used to indicate the beginning and end of class, or a function, or a code block. It's important that everything in your class is inside of these { } curly brackets.

    The Update and Start functions are optional. You don't need those.

    By the way, In Unity, the name of the class in your class definition must always be the same as the script's filename (That's a Unity rule, not a C# rule). In your case, your file is called "Level Finish.cs", but in C#, class names cannot have spaces in them, so you'll have to change the name to "LevelFinish.cs".

    All of this is explained in more detail here:

    https://docs.unity3d.com/Manual/CreatingAndUsingScripts.html
     
  7. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You need to declare a class. Also, C# uses a return type on methods. In this case, you do not need to return anything so it would be void.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3.  
    4. public class MyClass : MonoBehaviour
    5. {
    6.     private void OnTriggerEnter(Collider collider)
    7.     {
    8.         if (collider.tag == "Player")
    9.         {
    10.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    11.         }
    12.     }
    13. }
     
    Bunny83 likes this.
  8. Anonymous011

    Anonymous011

    Joined:
    May 13, 2020
    Posts:
    1
    i have the same error too how do i fix it help pls
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Don't hijack a one+-year old script for what is essentially a new question.

    I recommend making a new question, and here are some tips on how to report an issue:

    http://plbm.com/?p=220
     
    Bunny83 likes this.
  10. alihamoudalbasha

    alihamoudalbasha

    Joined:
    Nov 30, 2020
    Posts:
    2
    help i get ',' expected


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class Score : MonoBehaviour
    {
    public int i == 0f;
    public Text ScoreCount;
    void Update()
    {
    while(i <= 99)
    {
    StartCoroutine(scoreManager());
    i++;
    }

    ScoreCount.text == i;
    }
    IEnumerator scoreManager()
    {
    yield return new WaitForSeconds(1);
    }
    }
     
  11. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    Start your own post instead of replying to the first thread that comes up on google.
    When you do, post using code tags, and copy and paste the full error message instead of summarizing it.

    At a glance though I can see that you're using == where you should be using =. One equals sign is assignment, two equals signs are comparison (for use in "if" statements).
     
    Bunny83 and eses like this.
  12. alihamoudalbasha

    alihamoudalbasha

    Joined:
    Nov 30, 2020
    Posts:
    2
  13. loganfrison207

    loganfrison207

    Joined:
    Dec 10, 2020
    Posts:
    4
    est ce que vous pouvez m'aider svp mon code m affiche erreur CS1003

    this is my code
    classe publique TankHealth: MonoBehaviour;
    {
    flotteur public m_StartingHealth = 100f; // La quantité de santé avec laquelle chaque tank commence.
    public Slider m_Slider; // Le curseur pour représenter la santé du réservoir actuellement.
    Image publique m_FillImage; // Le composant image du curseur.
    public Color m_FullHealthColor = Color.green; // La couleur de la barre de santé sera en pleine santé.
    public Color m_ZeroHealthColor = Color.red; // La couleur de la barre de santé sera en cas d'absence de santé.
    public GameObject m_ExplosionPrefab; // Un préfabriqué qui sera instancié dans Awake, puis utilisé chaque fois que le tank meurt.


    AudioSource privé m_ExplosionAudio; // La source audio à jouer lorsque le tank explose.
    Private ParticleSystem m_ExplosionParticles; // Le système de particules jouera lorsque le tank sera détruit.
    flotteur privé m_CurrentHealth; // De combien de santé le tank a actuellement.
    booléen privé m_Dead; // Le tank a-t-il encore été réduit au-delà de zéro santé?


    privé vide Awake ()
    {
    // Instanciez le préfabriqué d'explosion et obtenez une référence au système de particules dessus.
    m_ExplosionParticles = Instancier (m_ExplosionPrefab) .GetComponent <ParticleSystem> ();

    // Récupère une référence à la source audio sur le préfabriqué instancié.
    m_ExplosionAudio = m_ExplosionParticles.GetComponent <AudioSource> ();

    // Désactivez le préfabriqué pour qu'il puisse être activé quand c'est nécessaire.
    m_ExplosionParticles.gameObject.SetActive (faux);
    }


    private void OnEnable ()
    {
    // Lorsque le réservoir est activé, réinitialisez la santé du réservoir et s'il est mort ou non.
    m_CurrentHealth = m_StartingHealth;
    m_Dead = faux;

    // Mettre à jour la valeur et la couleur du curseur de santé.
    SetHealthUI ();
    }


    public void TakeDamage (montant flottant)
    {
    // Réduit la santé actuelle du montant des dégâts infligés.
    m_CurrentHealth - = montant;

    // Modifiez les éléments de l'interface utilisateur de manière appropriée.
    SetHealthUI ();

    // Si l'état de santé actuel est égal ou inférieur à zéro et qu'il n'a pas encore été enregistré, appelez OnDeath.
    si (m_CurrentHealth <= 0f &&! m_Dead)
    {
    OnDeath ();
    }
    }


    private void SetHealthUI ()
    {
    // Définit la valeur du curseur de manière appropriée.
    m_Slider.value = m_CurrentHealth;

    // Interpole la couleur de la barre entre les couleurs choisies en fonction du pourcentage actuel de la santé de départ.
    m_FillImage.color = Color.Lerp (m_ZeroHealthColor, m_FullHealthColor, m_CurrentHealth / m_StartingHealth);
    }


    private void OnDeath ()
    {
    // Définit le drapeau pour que cette fonction ne soit appelée qu'une seule fois.
    m_Dead = vrai;

    // Déplacez le préfabriqué d'explosion instancié vers la position du réservoir et activez-le.
    m_ExplosionParticles.transform.position = transform.position;
    m_ExplosionParticles.gameObject.SetActive (vrai);

    // Joue le système de particules du réservoir qui explose.
    m_ExplosionParticles.Play ();

    // Joue l'effet sonore de l'explosion du réservoir.
    m_ExplosionAudio.Play ();

    // Éteignez le réservoir.
    gameObject.SetActive (faux);
    }
    }
     
  14. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    Looks like you pasted a C# script in Google translate. That's not valid C# at all. Please look up a basic C# tutorial.
     
    Bunny83 likes this.
  15. aizathossain

    aizathossain

    Joined:
    Dec 4, 2020
    Posts:
    4
    plz help

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Arms : MonoBehaviour
    {
    int speed = 3000;
    public Rigidbody2D rb;
    public Camera cam;
    public KeyCode mousebutton;

    void Update()
    {
    Vector3 playerpos = new Vector3(cam.ScreenToWorldPoint(Input.mousePosition).x,cam.ScreenToWorldPoint(Input.mousePosition).y 0);
    Vector3 difference = playerpos - transform.position;
    float rotationZ = Mathf.Atan2(difference.x, -difference.y) * Mathf.Rad2Deg;
    if (Input.GetKey(mousebutton))
    {
    rb.MoveRotation(Mathf.LerpAngle(rb.rotation, rotationZ, speed * Time.fixedDeltaTime));
    }
    }
    }
     
  16. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,539
    You didn't even read any of the content of this thread, right? It's the same thing again: Don't try to hijack an ancient thread, create your own thread. When you do, put some efford into your thread by explaining what you want to do, what doesn't work or what you want to know. Finally when posting code, use code tags.
     
  17. JuniorMaphosa

    JuniorMaphosa

    Joined:
    Feb 13, 2021
    Posts:
    7
    So do I also need to start a new thread? My error is below:

    Assets\Scripts\SupersonicEvents.cs(66,51): error CS1003: Syntax error, ',' expected
    Assets\Scripts\SupersonicEvents.cs(67,66): error CS1003: Syntax error, ',' expected

    Code (CSharp):
    1. // using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SupersonicEvents : MonoBehaviour
    6. {
    7.     void OnEnable()
    8.     {
    9.         SupersonicEvents.onOfferwallInitSuccessEvent += OfferwallInitSuccessEvent;
    10.  
    11.         SupersonicEvents.onOfferwallInitFailEvent += OfferwallInitFailEvent;
    12.  
    13.         SupersonicEvents.onOfferwallOpenedEvent += OfferwallOpenedEvent;
    14.  
    15.         SupersonicEvents.onOfferwallAdCreditedEvent += OfferwallAdCreditedEvent;
    16.  
    17.         SupersonicEvents.onOfferwallClosedEvent += OfferwallClosedEvent;
    18.  
    19.         SupersonicEvents.onOfferwallShowFailEvent += OfferwallShowFailEvent;
    20.  
    21.         SupersonicEvents.onGetOfferwallCreditsFailEvent += GetOfferwallCreditsFailEvent;
    22.     }
    23.    
    24.    
    25.     void OnDisable()
    26.     {
    27.         SupersonicEvents.onOfferwallInitSuccessEvent -= OfferwallInitSuccessEvent;
    28.  
    29.         SupersonicEvents.onOfferwallInitFailEvent -= OfferwallInitFailEvent;
    30.  
    31.         SupersonicEvents.onOfferwallOpenedEvent -= OfferwallOpenedEvent;
    32.  
    33.         SupersonicEvents.onOfferwallAdCreditedEvent -= OfferwallAdCreditedEvent;
    34.  
    35.         SupersonicEvents.onOfferwallClosedEvent -= OfferwallClosedEvent;
    36.  
    37.         SupersonicEvents.onOfferwallShowFailEvent -= OfferwallShowFailEvent;
    38.  
    39.         SupersonicEvents.onGetOfferwallCreditsFailEvent -= GetOfferwallCreditsFailEvent;
    40.  
    41.     }
    42.    
    43.    
    44.         /**
    45.         * Invoked when the Offerwall is prepared an ready to be shown to the user
    46.         */
    47.         void OfferwallInitSuccessEvent();
    48.         /**
    49.         * Invoked when the Offerwall does not load for the user
    50.         */
    51.         void OfferwallInitFailEvent(SupersonicError error);
    52.         /**
    53.         * Invoked when the Offerwall successfully loads for the user.
    54.         */
    55.         void OfferwallOpenedEvent();
    56.         /**
    57.         * Invoked when the method 'showOfferWall' is called and the OfferWall fails to load.  //@param desc - A string which represents the reason of 'showOfferwall' failure.
    58.         */
    59.         void OfferwallShowFailEvent(SupersonicError error);
    60.         /**
    61.         * Invoked each time the user completes an Offer.
    62.         * Award the user with the credit amount corresponding to the value of the ‘credits’
    63.         * parameter.
    64.         * @param dict - A dictionary which holds the credits and the total credits.  
    65.         */
    66.         void OfferwallAdCreditedEvent(Dictionary<string object> dict){
    67.         Debug.Log ("I got OfferwallAdCreditedEvent, current credits = "dict["credits"] + "totalCredits = " + dict["totalCredits"]);
    68.         }
    69.         /**
    70.         * Invoked when the method 'getOfferWallCredits' fails to retrieve
    71.         * the user's credit balance info.
    72.         * @param desc - A string object which represents the reason of 'getOffereallCredits' failure.
    73.         */
    74.         void GetOfferwallCreditsFailEvent(SupersonicError error);
    75.         /**
    76.         * Invoked when the user is about to return to the application after closing
    77.         * the Offerwall.
    78.         */
    79.         void OfferwallClosedEvent();
    80. }
     
  18. JuniorMaphosa

    JuniorMaphosa

    Joined:
    Feb 13, 2021
    Posts:
    7
     
  19. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,539
    I talked about a forum thread, not about code -.-. You just did the same thing as aizathossain did. You just bumped an unrelated thread with your own question. If you have a question, just create your own seperate post.
     
    Kurt-Dekker likes this.
  20. JuniorMaphosa

    JuniorMaphosa

    Joined:
    Feb 13, 2021
    Posts:
    7
    I also have the same error message of - ..."," - which is why I added my code here as it is relevant to the thread which is Syntax error, ',' expected. So I don't understand why I have to create a new thread.
     
  21. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    730
    The error message is the same, but the cause is different.
    You have typos in your code. The compiler tells you where they are, and does its best to guess what the problem is.

    Following the error message: look at lines 66 and 67. You can see that you are missing a comma from your dictionary parameter, and a "+" in your debug statement.
     
    Last edited: Feb 23, 2021
    JuniorMaphosa and Bunny83 like this.
  22. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Yes. You're barfing irrelevant crud all over an old thread, turning the knowledge base into mud.

    I can tell you EXACTLY what your error is... it's trivial... but create a fresh post and do it properly.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    Bunny83 likes this.
  23. JuniorMaphosa

    JuniorMaphosa

    Joined:
    Feb 13, 2021
    Posts:
    7
    Alright guys sorry for any inconvenience be sure out check out the links there Kurt
     
    Kurt-Dekker likes this.