Search Unity

I need help I keep getting this error with c sharp scripting!

Discussion in 'Scripting' started by MrMasterKeyboard, Apr 15, 2021.

  1. MrMasterKeyboard

    MrMasterKeyboard

    Joined:
    Apr 15, 2021
    Posts:
    5
    I am trying to program movement and camera movement too but I get this error: A local variable or function named 'x' is already defined in this scope. Error CS0128. Any bit of code that has x specified in it I will put here: float x = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;, float x = Input.GetAxisRaw("Horizontal");, Vector3 moveBy = transform.right * x + transform.forward * z; and transform.Rotate(0f, x, 0f);. Some help will be highly appreciated! Thank you.
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    You define x twice. You cant do that. When you want to access x, how would the compiler know which of the two values you mean? Also, when posting code please use code tags in the future :)
     
  3. You shouldn't define the same variable more than once in a scope.
    This is WRONG:
    Code (CSharp):
    1. float x = 1f;
    2. float x = 2f;
    If you want to overwrite the value of a variable, this is the RIGHT way:
    Code (CSharp):
    1. float x = 1f;
    2. x = 2f;
     
  4. MrMasterKeyboard

    MrMasterKeyboard

    Joined:
    Apr 15, 2021
    Posts:
    5
    I am a beginner to unity and c sharp altogether but my code is for scripting camera movement and movement from this site. Unity 3D FPS Movement (Beginner Friendly Tutorial) | Craft Games. Their code I am having a problem with is this bit:
    Code (CSharp):
    1. float x = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;
    2.     float y = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime * -1f;
    ;
     
  5. MrMasterKeyboard

    MrMasterKeyboard

    Joined:
    Apr 15, 2021
    Posts:
    5
    Wait that did the trick! Thanks!
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    First of all, the above code is NOT the same as the code you first typed. Read the above posts by Yoreki and Ninja carefully again.

    Second of all, rather than "i am having a problem," here is 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/