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

Error CS0128: A local variable or function named 'mouseX' is already defined in this scope

Discussion in 'Scripting' started by R-ty_dragon1, Jul 14, 2022.

  1. R-ty_dragon1

    R-ty_dragon1

    Joined:
    Aug 3, 2021
    Posts:
    50
    Hello,

    I am trying to move the camera angle to the mouse so it looks towards the mouse. My code is as follows;
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraLook : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.        
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         float mouseX = (Input.mousePosition.x / Screen.width) - 0.5f;
    17.         float mouseX = Mathf.Clamp(mouseX, -45, 45);
    18.         float mouseY = -12.754f;
    19.         transform.localRotation = Quaternion.Euler(new Vector4(-1f * (mouseY), mouseX * 360f, transform.localRotation.z));
    20.     }
    21. }
    22.  
    But in the console and error is displayed;

    Assets\Scripts\CameraLook.cs(17,15): error CS0128: A local variable or function named 'mouseX' is already defined in this scope


    Please help me.
    Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Seriously?

    Look at line 16. You declare mouseX

    Look at line 17. You declare mouseX

    Remove the word
    float
    on line 17

    And please, you can fix your own copy/pasta typos:

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.
     
    Bunny83 and R-ty_dragon1 like this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    The error is pretty clear about your issue. "A local variable or function named 'mouseX' is already defined".

    It tells you clearly that you've defined the variable already. And if you look at your code, you have two variables named mouseX. Now, your confusion may be that you want the same mouseX to be updated, but when you declare the type in front of it, that makes it think you want a new variable named mouseX. Don't declare float if you don't want it to be a new variable.
     
    Bunny83 and R-ty_dragon1 like this.
  4. R-ty_dragon1

    R-ty_dragon1

    Joined:
    Aug 3, 2021
    Posts:
    50
    Thanks sorry didn't notice that. It's changed but the code doesn't work the camera still goes 360 degrees all the way round