Search Unity

Other Need help with CS0128

Discussion in 'Scripting' started by HolyWaddaWadda, Oct 11, 2022.

  1. HolyWaddaWadda

    HolyWaddaWadda

    Joined:
    Aug 17, 2021
    Posts:
    10
    Code (CSharp):
    1. private void Update()
    2.     {
    3.         // Emits a ray in the direction the player is facing
    4.         Vector3 direction = Vector3.forward;
    5.         Ray theRay = new Ray(transform.position, transform.TransformDirection(direction * range));
    6.  
    7.         // Draws the ray in the scene view
    8.         Debug.DrawRay(transform.position, transform.TransformDirection(direction * range));
    9.  
    10.         if (Physics.Raycast(theRay, out RaycastHit hit, range))
    11.         {
    12.             if (hit.collider.tag == "Photo target")
    13.             {
    14.                 print("Take a picture.");
    15.             }
    16.         }
    17.  
    18.         // When left mouse button is pressed the camera takes a picture
    19.         if (Input.GetMouseButtonDown(0) && (Physics.Raycast(theRay, out RaycastHit hit, range)))
    20.         {
    21.             // If not viewing a picture a picture is taken
    22.             if (!viewingPhoto)
    23.             {
    24.                 StartCoroutine(CapturePhotoWithMoney());
    25.             }
    26.             // If viewing a picture the picture is removed from the screen
    27.             else
    28.             {
    29.                 RemovePhoto();
    30.             }
    31.         }
    The CS0128 says "A local variable or function called 'hit' is already defined in this scope" and it referres to the hit on the if (Input.GetMouseButtonDown(0) && (Physics.Raycast(theRay, out RaycastHit hit, range))) line
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    Yes, you define hit twice as the error states. On line 10 initially then on line 19. In C# when you define the type and arg name it defines it for you there and then. This is a convenience thing but you cannot do it twice; you can't define two things of the same name in the same scope.

    Just put "RaycastHit hit;" on Line 9 then just pass in "out hit" on lines 10 and 19.

    See: https://www.codeproject.com/Tips/1175809/Csharp-New-Inline-Out-Variables

    Forget error codes, don't even post them on the forums. Just read the error description.
     
  3. HolyWaddaWadda

    HolyWaddaWadda

    Joined:
    Aug 17, 2021
    Posts:
    10
    Thank you, it worked and i'll try to remember to not post the error codes
     
    MelvMay likes this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    You can post them but they are not the important part unlike the description and the line numbers etc. You use code-tags which is great, a lot of devs don't! :)
     
    Last edited: Oct 11, 2022
    Yoreki likes this.
  5. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Just to add a bit of context to what MelvMay said: a lot of people only post the error codes. Which is also, for example, done in the title here. The error code is useless and holds "no" information. Also, noone memorizes them (for the same reason). The important parts are the description of the error, which also contains the line numbers. This tells us what and where (specifically for your code) the problem occurs, and is usually very accurate (unless there is a structural problem, which usually makes the compiler incapable of correctly interpreting the file).
    This is also why using code tags is fantastic, since it adds line numbers. So posting the full error message and the relevant code using code tags gives anybody reading it all the information necessary to resolve the error. At least from a technical viewpoint.
     
    Last edited: Oct 11, 2022
    Kurt-Dekker and MelvMay like this.