Search Unity

error CS1003: Syntax Error, ',' expected

Discussion in 'Scripting' started by Alpaaca, Mar 28, 2020.

Thread Status:
Not open for further replies.
  1. Alpaaca

    Alpaaca

    Joined:
    Mar 28, 2020
    Posts:
    3
    Hey, I'm very new to unity and coding and I'm trying to follow a tutorial but the code that I use keeps coming up with this error:

    assets\scripts\Gunfire.sc(5,37): error CS1003: Syntax Error, ',' expected

    Idk what it means or how to fix it, I searched up lots of solutions but nothing works

    heres the script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Gunfire : MonoBehaviour
    6.  
    7. void Update () {
    8.         if(Input.GetButtonDown("Fire1")) {
    9.             AudioSource gunsound = GetComponent<AudioSource>();
    10.             gunsound.Play();
    11.         GetComponent<Animation>().Play("GunShot");
    12.         }
    13.     }
     
    vsachi339, dimasaestudios and johnc81 like this.
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    Add { to line 6
    Add } to line 14

    Also, is this your entire script? If not, paste the entire script bellow.
     
  3. Alpaaca

    Alpaaca

    Joined:
    Mar 28, 2020
    Posts:
    3
    It worked, thank you!

    Also for future reference, what should I look out for if that error comes up again

    (Also, that was the entire script)
     
  4. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    Well, the error means that you missed ; somewhere in your code. However, you didn't actually miss a ; but { }. There's really nothing specific to look out for in the future, just retrace your steps and try locating that syntax error.
     
  5. Alpaaca

    Alpaaca

    Joined:
    Mar 28, 2020
    Posts:
    3
    Thank you for you help, man.
     
  6. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    If you have decent syntax errors like missing ; or braces the errors can come in all shapes and sizes. In general clicking on the error will get you close to where the issue is, and then just make sure the braces are correct (hard part). If you have something like 20 errors showing, always start at the first one, as its possible all the others are related due to a missing brace and will go away by themselves.
     
    Nialler2017, bartolinoah and Dextozz like this.
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    ¿Ejecutó google translate para traducir su código al español ...?

    Haga una nueva publicación (no responda a una anterior con su propio problema), publique el código real (sin traducción) y cuando la publique, use etiquetas de código como esta:
    [code ]code here[/code]
     
  8. gtonuimatwek

    gtonuimatwek

    Joined:
    Sep 19, 2020
    Posts:
    4
    Assets\Scripts\Systems\HUDController.cs(5,44): error CS1003: Syntax error, ',' expected
    I am getting this errror everytime can someone help. here is my code



    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;

    public class HUDController : MonoBehaviour : UnityEngine.UI {


    public UI.Text Score;
    public UI.Text BallsLeftText;
    public UI.Text ScoreText;
    public UI.Image Logomarca;
    public UI.Text BallsLeft;



    // Use this for initialization
    void Start () {
    int scWidth = Screen.width;

    if (scWidth > 1200) {
    Score.transform.position = new Vector3 (Score.transform.position.x-0.05f, Score.transform.position.y, Score.transform.position.z);
    Score.fontSize = 80;
    ScoreText.transform.position = new Vector3 (ScoreText.transform.position.x-0.02f, ScoreText.transform.position.y, ScoreText.transform.position.z);
    ScoreText.fontSize = 140;
    BallsLeft.transform.position = new Vector3 (BallsLeft.transform.position.x-0.05f, BallsLeft.transform.position.y, BallsLeft.transform.position.z);
    BallsLeft.fontSize = 80;
    BallsLeftText.transform.position = new Vector3 (BallsLeftText.transform.position.x-0.02f, BallsLeftText.transform.position.y, BallsLeftText.transform.position.z);
    BallsLeftText.fontSize = 140;
    }

    //(Screen.height / defaultScreenSize) * defaultFontSize

    // GUI.Label (new Rect (scWidth/2.6f, scHeigth/7, scWidth/4, scHeigth/4), msgTexture);
    }


    // Update is called once per frame
    void Update () {

    }

    }



    thanks in advance
     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    1. Post your own thread instead of posting a reply to someone else's
    2. Use code tags
    3. You have ": UnityEngine.UI" in your "public class...." line, it shouldn't be there. Look at any other script file for an example of what that line is supposed to look like.
     
    Kurt-Dekker likes this.
  10. trexeratot

    trexeratot

    Joined:
    Nov 19, 2020
    Posts:
    1
    I am doing a player camera movment and i am gating the same error
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class mouselock : MonoBehaviour
    {

    public float mouseSensitivity = 100f;

    public Transform playerBody;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

    playerBody.Rotate(Vecter 3.up * mouseX);
    } }
     
  11. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    Vecter 3.up
    should be
    Vector3.up

    Also, use code tags when posting your code, and perhaps create your own topic next time with the actual error message copied into it.
     
  12. AquaticNub

    AquaticNub

    Joined:
    Dec 5, 2019
    Posts:
    2
    Hey I got the same error and i cant seem to find it i was learning how to use a grapple hook and then I got the same error( Im new to unity aswell)
    using UnityEngine;

    public class GrapplingGun : MonoBehaviour
    {
    private LineRenderer LR;
    private Vector3 grapplePoint;
    public LayerMask whatIsGrappleable;
    public Transform gunTip, camera, player;
    private float maxDistance = 100f;
    private SpringJoint joint;

    void Awake()
    {
    LR = GetComponent<LineRenderer>();
    }

    void Update()
    {
    if (Input.GetMouseButtonDown(0))
    {
    StartGrapple();
    }
    else if (Input.GetMouseButtonUp(0))
    {
    StopGrapple();
    }

    void StartGrapple()
    {
    RaycastHit hit;
    if(Physics.Raycast(origin: camera.position, direction: camera.forward out hit, maxDistane, whatIsGrappleable))
    {
    grapplePoint = hit.point;
    joint = player.gameObject.AddComponent<SpringJoint>();
    joint.autoConfigureConnectedAnchor = false;
    joint.connectedAnchor = grapplePoint;

    float distanceFromPoint = Vector3.Distance(a: player.position, b: grapplePoint);

    joint.maxDistance = distanceFromPoint * 0.8f;
    joint.minDistance = distanceFromPoint * 0.25f;

    joint.spring = 4.5f;
    joint.damper = 7f;
    joint.massScale = 4.5f;
    }
    }

    void StopGrapple()
    {

    }



    }
    }
     
  13. AquaticNub

    AquaticNub

    Joined:
    Dec 5, 2019
    Posts:
    2
    nevermind
     
  14. starterunity

    starterunity

    Joined:
    Dec 2, 2020
    Posts:
    1
    Yeah its like that but i got thr problem too man
     
  15. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    This is completely wrong.
    Code (csharp):
    1. move Direction = new Vector3("xDirection, 0.0f, zDirection");
    You do not put "" around the values.
    Change it to:
    Code (csharp):
    1. move Direction = new Vector3(xDirection, 0.0f, zDirection);
     
  16. antonijantonij2005

    antonijantonij2005

    Joined:
    Dec 16, 2020
    Posts:
    1
    hi im new to this unity stuff.
    i wrote a code that i found on youtube and it gives me; error CS1003: Syntax error, ',' expected
    here is the code but i cant find the problem

    using UnityEngine;

    public class MouseLook : MonoBehaviour
    {
    public float mouseSensitivity - 100f;

    public Transform playerBody;

    float xRotation = 0f;
    // Start is called before the first frame update
    void Start()
    {
    Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
    float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

    xRotation -= mouseY;
    xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    playerBody.Rotate(Vector3.up * mouseX);
    }

    }
     
    cejuan08 likes this.
  17. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481
    Your line: public float mouseSensitivity - 100f;

    should read: public float mouseSensitivity = 100f;
     
  18. thekillerfgh

    thekillerfgh

    Joined:
    Dec 19, 2020
    Posts:
    2
    Hi I am new to unity i can't fix this error plz help

    public class PlayerController : MonoBehaviour
    {
    public float horizontalInput;
    public float speed=10.0f;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    if(transform.position.x < -15);
    {
    transform.position =new Vector3 (-15, transform.position.y transform.position.z);
    }
    horizontalInput =Input.GetAxis("Horizontal");
    transform.Translate(Vector3.right * horizontalInput * Time.deltaTime * speed);
    }
    }
     
  19. AriyaSD

    AriyaSD

    Joined:
    Nov 2, 2019
    Posts:
    24
    You're missing a comma on this line:
    Code (CSharp):
    1. transform.position =new Vector3 (-15, transform.position.y transform.position.z);
    It should be:
    Code (CSharp):
    1. transform.position =new Vector3 (-15, transform.position.y, transform.position.z);
    Also, your if statement won't do anything because you put a semicolon at the end. The code block below it will always run.
     
    thekillerfgh likes this.
  20. thekillerfgh

    thekillerfgh

    Joined:
    Dec 19, 2020
    Posts:
    2
    It fixed the issue thank you so much AriyaSD
     
  21. yurovsky98

    yurovsky98

    Joined:
    Nov 23, 2018
    Posts:
    3
    I have the same error but I double checked my code. Also I double clicked on an error and it's do not show exactly where the problem occurred.

    Assets\Scripts\PlayerController.cs(12,25): error CS1003: Syntax error, ',' expected
    Assets\Scripts\PlayerController.cs(13,30): error CS1003: Syntax error, ',' expected

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     private float startSense = 2.96f;
    9.     private float startEnergy = 50.0f;
    10.     private float startMovementSpeed = 3.0f;
    11.  
    12.     public float movementSpeed = 3.0f;                                                      //movement speed of the player
    13.     public float energy 50.0f;                                                             //energy of the player
    14.     private float tempEnergy 50.0f;
    15.     private float senseRange;
    16.  
    17.     private bool alreadyTriggered;                                                  //to check if it olready have food to go to
    18.     private bool mooveRand;                                                          //to check if player has to move randomly
    19.     private bool alive;                                                              //to check if player is alive
    20.  
    21.     public int EatenFood = 0;                                                               //cont of food eaten by player
    22.  
    23.     private GameObject Game_Controller;                                                     //game controller
    24.     private GameController ScriptFromGC;
    25.  
    26.  
    27.     private SphereCollider SphereCollaider;
    28.  
    29.     private Vector3 FoodPos;                                                                //position of seened food
    30.     private Vector3 RandTarget;                                                             //position to move randomly
    31.     private Renderer BlobRender;
    32.     public Color altColor = Color.red;
    33.  
    34.     private bool firstGeneration = true;
    35.  
    36.     private void Start()                                                                    //called befare first frame
    37.     {
    38.  
    39.         GameObject senseGo = transform.Find("Sense").gameObject;
    40.         var sphere = senseGo.GetComponent<SphereCollider>();
    41.         senseRange = sphere.radius;
    42.         sphere.radius = startSense;
    43.         movementSpeed = startMovementSpeed;
    44.         energy = tempEnergy;
    45.         EatenFood = 0;
    46.  
    47.  
    48.         BlobRender = GetComponent<Renderer>();
    49.         RandTarget = new Vector3(Random.Range(10, -8), 1.9f, Random.Range(-11, 8));         //create a first random position to move to
    50.         GameObject go = GameObject.Find("GameHelper");                                      //finding game object with teg - GameHelper
    51.         ScriptFromGC = (GameController)go.GetComponent(typeof(GameController));             //finding script on GameHelper object
    52.         alreadyTriggered = false;
    53.         mooveRand = true;
    54.         alive = true;
    55.     }
    56.  
    57.     void Update()                                                                           //called each frame(if FPS of the game 30 called 30 times per second)
    58.     {
    59.         if (alive)                                                                          //checks if playesr alive
    60.         {
    61.             if (mooveRand)                                                                  //checks if playesr should move randomly
    62.             {
    63.                 transform.position = Vector3.MoveTowards(transform.position, RandTarget, Time.deltaTime * movementSpeed);   //moves player to random position
    64.  
    65.                 if (transform.position == RandTarget)                                       //if player has come to rand position
    66.                 {
    67.                     RandTarget = new Vector3(Random.Range(10, -8), 2.01f, Random.Range(-11, 8)); //create new random position to go to
    68.                     transform.position = Vector3.MoveTowards(transform.position, RandTarget, Time.deltaTime * movementSpeed);
    69.                 }
    70.             }
    71.             else
    72.             {
    73.                 transform.position = Vector3.MoveTowards(transform.position, FoodPos, Time.deltaTime * movementSpeed);      //moves player to food
    74.  
    75.                 if (transform.position == FoodPos)                                          //if it came to food position
    76.                 {
    77.                     alreadyTriggered = false;                                               //can find anouther food
    78.                     mooveRand = true;                                                       //player starts to move randomly
    79.                 }
    80.             }
    81.  
    82.             if (energy <= 0 || EatenFood >= 2)                                                                     //if energy is gone
    83.             {
    84.                 alive = false;                                                                  //player die
    85.             }
    86.             else
    87.             {
    88.                 energy -= 0.1f;                                                                 //reduse energy while alive
    89.             }
    90.  
    91.         }
    92.         else
    93.         {
    94.             ScriptFromGC.KilledOpponent(this.gameObject);                                   //calls function in game controller script to remove this player from list
    95.         }
    96.     }
    97.  
    98.     private void OnTriggerStay(Collider FoodPosition)                                       //called when sence find food
    99.     {
    100.         if (alreadyTriggered == false)                                                      //check if player allready find food to prevent it from comming to another one
    101.         {
    102.             alreadyTriggered = true;                                                        //can find anouther food
    103.             mooveRand = false;                                                              //player starts to move randomly
    104.             FoodPos = FoodPosition.transform.position;                                      //sets to FooodPos position of the food
    105.         }
    106.     }
    107.  
    108.     void OnCollisionEnter(Collision collision)                                              //calls when hits food
    109.     {
    110.         if (collision.gameObject.tag == "Food")
    111.         {
    112.             EatenFood += 1;                                                                     //add food to count
    113.         }
    114.     }
    115.  
    116.     public void mutation()
    117.     {
    118.         GameObject senseGo = transform.Find("Sense").gameObject;
    119.         var sphere = senseGo.GetComponent<SphereCollider>();
    120.         Debug.Log("Movement speed is " + movementSpeed + "start movement speed is " + startMovementSpeed);
    121.         Debug.Log("radius is " + sphere.radius + "start radius is " + startSense);
    122.         Debug.Log("energy is " + energy + "start energy is " + startEnergy);
    123.         if (movementSpeed != startMovementSpeed)
    124.         {
    125.             int x = Random.Range(1, 101);
    126.             if (x <= 94)
    127.             {
    128.                 speedMutation();
    129.             }
    130.             else
    131.             {
    132.                 randomMutation();
    133.             }
    134.         }
    135.         else if (sphere.radius != startSense)
    136.         {
    137.             int x = Random.Range(1, 101);
    138.             if (x <= 94)
    139.             {
    140.                 senseMutation();
    141.             }
    142.             else
    143.             {
    144.                 randomMutation();
    145.             }
    146.         }
    147.         else if (tempEnergy != startEnergy)
    148.         {
    149.             int x = Random.Range(1, 101);
    150.             if (x <= 94)
    151.             {
    152.                 energyMutation();
    153.             }
    154.             else
    155.             {
    156.                 randomMutation();
    157.             }
    158.         }
    159.         else
    160.             randomMutation();
    161.  
    162.     }
    163.  
    164.     public void senseMutation()
    165.     {
    166.         GameObject senseGo = transform.Find("Sense").gameObject;
    167.         var sphere = senseGo.GetComponent<SphereCollider>();
    168.         sphere.radius += 1f;
    169.         Debug.Log("Radius mutation");
    170.     }
    171.  
    172.     public void speedMutation()
    173.     {
    174.         movementSpeed += 1f;
    175.         Debug.Log("Speed mutation");
    176.     }
    177.  
    178.     public void energyMutation()
    179.     {
    180.         tempEnergy += 10f;
    181.         Debug.Log("Energy mutation");
    182.     }
    183.  
    184.     public void randomMutation()
    185.     {
    186.         int x = Random.Range(1, 4);
    187.         switch (x)
    188.         {
    189.             case 1:
    190.                 {
    191.                     senseMutation();
    192.                     break;
    193.                 }
    194.             case 2:
    195.                 {
    196.                     speedMutation();
    197.                     break;
    198.                 }
    199.             case 3:
    200.                 {
    201.                     energyMutation();
    202.                     break;
    203.                 }
    204.         }
    205.     }
    206. }
    207.  
     
    Last edited: Jan 14, 2021
  22. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    No that's actually not how it works.

    You have an error message in front of you.

    Look at the line number on the message.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220
     
    yurovsky98 likes this.
  23. yurovsky98

    yurovsky98

    Joined:
    Nov 23, 2018
    Posts:
    3
    I edited my massage. The main problem is that I really don't know where the problem is in my code.
     
  24. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    I'll say it again: look at the line number in the error message. This is how you find this stuff.

    From your posted code above, study line 12.

    Then study line 13 and 14 ... do you see anything missing in those second lines? :) It's only one character missing per line, but it's an important character.
     
    yurovsky98 likes this.
  25. yurovsky98

    yurovsky98

    Joined:
    Nov 23, 2018
    Posts:
    3
    OMG I am so dumb, thank you for help and sorry for disturbing you))
     
    Kurt-Dekker likes this.
  26. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    Of course! No problem. Glad you're back online... I want everybody to be able to help themselves as much as the compiler can help them. Come on back if you have anything that doesn't make sense!
     
  27. JDinoman

    JDinoman

    Joined:
    Jan 26, 2021
    Posts:
    26
    I need help.
    I know little about unity, so i decided to use the tutorials. I run Unity 2018.3 I cannot get the newer ones, don't ask.
    Anyway, the tutorial said specifically 2018.3 or above, and that's what i've got. I downloaded it and opened it, and i fixed most of the Compiler Errors by editing the C# Scripts, but i just can't figure out how to fix this one:

    Assets\Content\game\Scripts\PlayerAvatar.cs(9,57): error CS1003: Syntax error, ',' expected

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityStandardAssets.Characters.ThirdPerson;
    using UnityEngine.Events;

    [RequireComponent(typeof(Animator))]
    public class PlayerAvatar : MonoBehaviour, IPlayerAvatar, ;
    {
    public UnityEvent OnDeath;
    public UnityEvent OnRespawn;
    public float respawnDelay = 5;

    GameController gameController;
    Animator animator;
    WaitForSeconds delay;
    new CapsuleCollider collider;

    void Awake()
    {
    animator = GetComponent<Animator>();
    collider = GetComponent<CapsuleCollider>();
    delay = new WaitForSeconds(respawnDelay);
    gameController = FindObjectOfType<GameController>();
    }

    public void Die()
    {
    StartCoroutine(DieThenRespawn());
    }

    IEnumerator DieThenRespawn()
    {
    animator.SetBool("DeathTrigger", true);
    var controller = GetComponent<ThirdPersonUserControl>();
    if (controller != null)
    controller.enabled = false;
    collider.height = 0.2f;
    collider.center = new Vector3(0, 0.3f, 0);
    OnDeath.Invoke();
    yield return delay;
    OnRespawn.Invoke();
    animator.SetBool("DeathTrigger", false);
    animator.Rebind();
    if (controller != null)
    controller.enabled = true;
    collider.height = 1.6f;
    collider.center = new Vector3(0, 0.8f, 0);

    gameController.RestartLevel();
    }
    }
     
  28. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    Then please start your own fresh thread. It's free. Don't piggyback on other unrelated questions, you're just muddying the historical waters here on the forum.

    When you post, here is how to report your problem productively in the Unity3D forums:

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

    How to understand errors in general:

    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/
     
  29. JDinoman

    JDinoman

    Joined:
    Jan 26, 2021
    Posts:
    26
    Yeah okay I get it. I started a new thread.
     
    Kurt-Dekker likes this.
  30. AbdullahAlsuedani

    AbdullahAlsuedani

    Joined:
    Feb 18, 2021
    Posts:
    1
    Hey, I'm very new to unity and coding and I'm trying to follow a tutorial but the code that I use keeps coming up with this error:

    assets\scripts\Gunfire.sc(5,37): error CS1003: Syntax Error, ',' expected

    Idk what it means or how to fix it, I searched up lots of solutions but nothing works

    heres the script:
    upload_2021-2-18_16-32-15.png
     

    Attached Files:

  31. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    I'll post this again, since apparently people need the help.

    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, don't post a JPEG. Post the text, copy-pasted, and ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  32. Mishastopchet

    Mishastopchet

    Joined:
    Apr 13, 2021
    Posts:
    1
    Hey, I'm very new to unity and coding and I'm trying to follow a tutorial but the code that I use keeps coming up with this error:Assets\Scripts\HERO.cs(4,40): error CS1003: Syntax error, ',' expected
    Help please
    public class Hero : MonoBehaviour
    {
    [SerializeField] private float speed = 3f;
    [SerializeField] private int lives 5;
    [SerializeField] private float jumpForse = 15f;

    private Rigidbody2D rb;
    private SpriteRenderer sprite;

    private void Awake()
    {
    rb = GetComponent<Rigidbody2D>();
    sprite = GetComponentInChildren<SpriteRenderer>();
    }
    private void Update()
    {
    if (Input.GetButton("Horizontal"))
    Run();
    }
    private void Run()
    {
    Vector3 dir = transform.right * Input.GetAxis("Horisontal");
    transform.position = Vector3.MoveTowards(transform.position, transform.position + dir, speed * Time.deltaTime);
    }
    }
     
  33. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    Oh my goodness... absolutely incredible. Please read the post DIRECTLY above your post. The entire answer is right there, plus it explains how to post code properly.
     
    Last edited: Apr 13, 2021
    MelvMay likes this.
  34. progamerrrrrrr

    progamerrrrrrr

    Joined:
    Apr 17, 2021
    Posts:
    1
    i have a error

    Assets\Charecter Controler.cs(3,49): error CS1003: Syntax error, ',' expected
    That is my error and vertion is 2021.1.0

    This is my code

    using UnityEngine;

    public class CharecterController : MonoBehaviour;
    {

    [SerializeField] private float m_JumpForce = 400f; // Amount of force added when the player jumps.
    [Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f; // Amount of maxSpeed applied to crouching movement. 1 = 100%
    [Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f; // How much to smooth out the movement
    [SerializeField] private bool m_AirControl = false; // Whether or not a player can steer while jumping;
    [SerializeField] private LayerMask m_WhatIsGround; // A mask determining what is ground to the character
    [SerializeField] private Transform m_GroundCheck; // A position marking where to check if the player is grounded.
    [SerializeField] private Transform m_CeilingCheck; // A position marking where to check for ceilings
    [SerializeField] private Collider2D m_CrouchDisableCollider; // A collider that will be disabled when crouching

    const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
    private bool m_Grounded; // Whether or not the player is grounded.
    const float k_CeilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up
    private Rigidbody2D m_Rigidbody2D;
    private bool m_FacingRight = true; // For determining which way the player is currently facing.
    private Vector3 m_Velocity = Vector3.zero;

    [Header("Events")]
    [Space]

    public UnityEvent OnLandEvent;

    [System.Serializable]
    public class BoolEvent : UnityEvent<bool> { }

    public BoolEvent OnCrouchEvent;
    private bool m_wasCrouching = false;

    private void Awake()
    {
    m_Rigidbody2D = GetComponent<Rigidbody2D>();

    if (OnLandEvent == null)
    OnLandEvent = new UnityEvent();

    if (OnCrouchEvent == null)
    OnCrouchEvent = new BoolEvent();
    }

    private void FixedUpdate()
    {
    bool wasGrounded = m_Grounded;
    m_Grounded = false;

    // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
    // This can be done using layers instead but Sample Assets will not overwrite your project settings.
    Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
    for (int i = 0; i < colliders.Length; i++)
    {
    if (colliders.gameObject != gameObject)
    {
    m_Grounded = true;
    if (!wasGrounded)
    OnLandEvent.Invoke();
    }
    }
    }


    public void Move(float move, bool crouch, bool jump)
    {
    // If crouching, check to see if the character can stand up
    if (!crouch)
    {
    // If the character has a ceiling preventing them from standing up, keep them crouching
    if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
    {
    crouch = true;
    }
    }

    //only control the player if grounded or airControl is turned on
    if (m_Grounded || m_AirControl)
    {

    // If crouching
    if (crouch)
    {
    if (!m_wasCrouching)
    {
    m_wasCrouching = true;
    OnCrouchEvent.Invoke(true);
    }

    // Reduce the speed by the crouchSpeed multiplier
    move *= m_CrouchSpeed;

    // Disable one of the colliders when crouching
    if (m_CrouchDisableCollider != null)
    m_CrouchDisableCollider.enabled = false;
    } else
    {
    // Enable the collider when not crouching
    if (m_CrouchDisableCollider != null)
    m_CrouchDisableCollider.enabled = true;

    if (m_wasCrouching)
    {
    m_wasCrouching = false;
    OnCrouchEvent.Invoke(false);
    }
    }

    // Move the character by finding the target velocity
    Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
    // And then smoothing it out and applying it to the character
    m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);

    // If the input is moving the player right and the player is facing left...
    if (move > 0 && !m_FacingRight)
    {
    // ... flip the player.
    Flip();
    }
    // Otherwise if the input is moving the player left and the player is facing right...
    else if (move < 0 && m_FacingRight)
    {
    // ... flip the player.
    Flip();
    }
    }
    // If the player should jump...
    if (m_Grounded && jump)
    {
    // Add a vertical force to the player.
    m_Grounded = false;
    m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
    }
    }


    private void Flip()
    {
    // Switch the way the player is labelled as facing.
    m_FacingRight = !m_FacingRight;

    // Multiply the player's x local scale by -1.
    Vector3 theScale = transform.localScale;
    theScale.x *= -1;
    transform.localScale = theScale;
    }
    }
     
  35. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Good lord this thread is still going. It's like a magnet for oblivious people.

     
  36. Ryze4_20

    Ryze4_20

    Joined:
    Dec 27, 2020
    Posts:
    5
    Can someone help me? I have this error error CS1003: Syntax error, ',' expected

    my code is -



    using UnityEngine;

    public class movemntPl : MonoBehaviour
    {

    public Rigidbody rb;
    public float jumpw = 10f;
    public float cd = 1;
    bool cooldanw == true;


    // Update is called once per frame
    void FixedUpdate()
    {
    if (Input.GetKey("space")
    && cooldanw == true)
    {
    cooldanw == false;
    rb.AddForce(0, jumpw * Time.deltaTime, 1 * Time.deltaTime, ForceMode.Impulse);
    Invoke("ResTime", cd);
    }
    }

    public void ResTime()
    {
    cooldanw == true;
    }
    }
     
  37. Ryze4_20

    Ryze4_20

    Joined:
    Dec 27, 2020
    Posts:
    5
     
  38. Ryze4_20

    Ryze4_20

    Joined:
    Dec 27, 2020
    Posts:
    5
    This is my code if you can help me please do it!

    /Code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public Rigidbody rb;
    8.     public float jpower = 10f;
    9.     public float forwardsp = 10f;
    10.     public bool cd == true;
    11.     public float jumpcd;
    12.  
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         rb.AddForce(0, jpower * Time.deltaTime, forwardsp * Time.deltaTime, ForceMode.Impulse);
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void FixedUpdate()
    22.     {
    23.         if (Input.GetKey("space") && cd == true)
    24.             rb.AddForce(0, jpower * Time.deltaTime, forwardsp * Time.deltaTime, ForceMode.Impulse);
    25.         cd == false;
    26.         Invoke("resTime", jumpcd);
    27.     }
    28.  
    29.     void resTime ()
    30.     {
    31.         if (cd == false)
    32.         {
    33.             cd == true;
    34.         }
    35.      
    36.     }
    37. }
    38.  
     
  39. See? Babysteps. If you respect our time and effort, we will help you (although right now it sounds I'm referring to myself in plural, but it's the "rule of the land")

    Check the places where you're using
    ==
    outside of if statements. You're trying to assign value to variables using this. It should be single
    =
    sign.


    PS: and don't just watch that video, follow along and practice coding. You don't learn how to code overnight.
     
  40. Ryze4_20

    Ryze4_20

    Joined:
    Dec 27, 2020
    Posts:
    5
    Like this?
    PS:Ninja can we continue our talk in private messages?Because we will spam the comments and yea


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public Rigidbody rb;
    8.     public float jpower = 10f;
    9.     public float forwardsp = 10f;
    10.     public bool cd = true;
    11.     public float jumpcd;
    12.  
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         rb.AddForce(0, jpower * Time.deltaTime, forwardsp * Time.deltaTime, ForceMode.Impulse);
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void FixedUpdate()
    22.     {
    23.         if (Input.GetKey("space") && cd == true)
    24.         {
    25.             rb.AddForce(0, jpower * Time.deltaTime, forwardsp * Time.deltaTime, ForceMode.Impulse);
    26.             cd == false;
    27.             Invoke("resTime", jumpcd);
    28.         }
    29.     }
    30.  
    31.     void resTime ()
    32.     {
    33.         if (cd == false)
    34.         {
    35.             cd == true;
    36.         }
    37.      
    38.     }
    39. }
    40.  
     
    Last edited: May 22, 2021
  41. No. The whole thing of helping is that other people see it when they do the same mistakes they can learn from yours.

    No, what about these? Outside of the if statements... if statements are whatever inside the parenthesis: if(...here...), anything outside of it in your code has == is wrong.
    Good:
    if (cd == true)
    although you could just
    if(cd)
    or when you want
    if(cd == false)
    you can just
    if(!cd)

    Bad:
    cd == true;
    or
    cd == false;
     
    Ryze4_20 likes this.
  42. Ryze4_20

    Ryze4_20

    Joined:
    Dec 27, 2020
    Posts:
    5
    Lol my code work!Thanks you so much, and i will try to follow the rules
     
  43. AlexUniverse007

    AlexUniverse007

    Joined:
    Jun 7, 2021
    Posts:
    1
    Hi could someone help me see where my mistake is? here is my code


    Code (CSharp):
    1. using System.Collections;  
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Jugador : MonoBehaviour
    6. {
    7.     public int FuerzaDeSalto;
    8.     public int Velocidad;
    9.     bool EnElPiso false;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.        
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         if (Input.GetKeyDown("space") && EnElPiso)
    21.         {
    22.             this.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, FuerzaDeSalto));
    23.         }
    24.  
    25.         this.GetComponent<Rigidbody2D>().velocity = new Vector2(Velocidad, this.GetComponent<Rigidbody2D>().velocity.y);
    26.     }
    27.  
    28.     private void OnTriggerEnter2D(Collider2D collision)
    29.     {
    30.         EnElPiso = true;
    31.     }
    32.  
    33.     private void OnTriggerExit2D(Collider2D collision)
    34.     {
    35.         EnElPiso = false;
    36.     }
    37.  
     
  44. santiagomk31

    santiagomk31

    Joined:
    Jun 23, 2021
    Posts:
    1
    Alguíen me podría ayudar, me sale ese mismo error, este es mi codigo:
    Code (CSharp):
    1. [RequireComponent(tipe of(CharacterController))]
    2. public class PlayerController : MonoBehaviour
    3. {
    4.     public float walkSpeed = 5f;
    5.  
    6.     Vector3 moveInput = Vector3.zero;
    7.     CharacterController characterController;
    8.  
    9.  
    10.     private void Awake()
    11.     {
    12.         characterController = GetComponent<CharacterController>();
    13.     }
    14.  
    15.     private void Update()
    16.     {
    17.         Move();
    18.     }
    19.  
    20.     private void Move()
    21.     {
     
  45. UnityMaru

    UnityMaru

    Community Engagement Manager PSM

    Joined:
    Mar 16, 2016
    Posts:
    1,227
    I feel this thread has served it's purpose. There are many answers above on how to resolve this. Otherwise, this thread is just going to have repeated questions over and over.
     
Thread Status:
Not open for further replies.