Unexpected symbol `hit` Physics2D.Raycast

Trying to convert a script from 3D to 2D, it’s giving me an “Unexpected symbol `hit’” on this line:

if(RaycastHit2D hit = Physics2D.Raycast(ray.origin,ray.direction,100))//cast the ray 100 units at the specified direction

In 3D the code was this:

 if(Physics.Raycast(ray.origin,ray.direction, out hit, 100))//cast the ray 100 units at the specified direction

It’s correct according to the manual, not sure what’s wrong? Full code below.

using UnityEngine;
using System.Collections;

[RequireComponent (typeof (LineRenderer))]

public class RaycastReflection : MonoBehaviour
{
    //this game object's Transform
    private Transform goTransform;
    //the attached line renderer
    private LineRenderer lineRenderer;
   
    //a ray
    private Ray2D ray;
    //a RaycastHit variable, to gather informartion about the ray's collision
    private RaycastHit2D hit;
   
    //reflection direction
    private Vector3 inDirection;
   
    //the number of reflections
    public int nReflections = 2;
   
    //the number of points at the line renderer
    private int nPoints;
   
    void Awake ()
    {
        //get the attached Transform component
        goTransform = this.GetComponent<Transform>();
        //get the attached LineRenderer component
        lineRenderer = this.GetComponent<LineRenderer>();
    }
   
    void Update ()
    {
        //clamp the number of reflections between 1 and int capacity
        nReflections = Mathf.Clamp(nReflections,1,nReflections);
        //cast a new ray forward, from the current attached game object position
        ray = new Ray2D(goTransform.position,goTransform.up);
       
        //represent the ray using a line that can only be viewed at the scene tab
        Debug.DrawRay(goTransform.position,goTransform.up * 100, Color.magenta);
       
        //set the number of points to be the same as the number of reflections
        nPoints = nReflections;
        //make the lineRenderer have nPoints
        lineRenderer.SetVertexCount(nPoints);
        //Set the first point of the line at the current attached game object position
        lineRenderer.SetPosition(0,goTransform.position);
       
        for(int i=0;i<=nReflections;i++)
        {
            //If the ray hasn't reflected yet
            if(i==0)
            {
                //Check if the ray has hit something

                if(RaycastHit2D hit = Physics2D.Raycast(ray.origin,ray.direction,100))//cast the ray 100 units at the specified direction
                {
                    //the reflection direction is the reflection of the current ray direction flipped at the hit normal
                    inDirection = Vector3.Reflect(ray.direction,hit.normal);
                    //cast the reflected ray, using the hit point as the origin and the reflected direction as the direction
                    ray = new Ray2D(hit.point,inDirection);
                   
                    //Draw the normal - can only be seen at the Scene tab, for debugging purposes
                    Debug.DrawRay(hit.point, hit.normal*3, Color.blue);
                    //represent the ray using a line that can only be viewed at the scene tab
                    Debug.DrawRay(hit.point, inDirection*100, Color.magenta);
                   
                    //Print the name of the object the cast ray has hit, at the console
                    Debug.Log("Object name: " + hit.transform.name);
                   
                    //if the number of reflections is set to 1
                    if(nReflections==1)
                    {
                        //add a new vertex to the line renderer
                        lineRenderer.SetVertexCount(++nPoints);
                    }
                   
                    //set the position of the next vertex at the line renderer to be the same as the hit point
                    lineRenderer.SetPosition(i+1,hit.point);
                }
            }
            else // the ray has reflected at least once
            {
                //Check if the ray has hit something
                if(RaycastHit2D hit = Physics2D.Raycast(ray.origin,ray.direction,100))//cast the ray 100 units at the specified direction
                {
                    //the refletion direction is the reflection of the ray's direction at the hit normal
                    inDirection = Vector3.Reflect(inDirection,hit.normal);
                    //cast the reflected ray, using the hit point as the origin and the reflected direction as the direction
                    ray = new Ray2D(hit.point,inDirection);
                   
                    //Draw the normal - can only be seen at the Scene tab, for debugging purposes
                    Debug.DrawRay(hit.point, hit.normal*3, Color.blue);
                    //represent the ray using a line that can only be viewed at the scene tab
                    Debug.DrawRay(hit.point, inDirection*100, Color.magenta);
                   
                    //Print the name of the object the cast ray has hit, at the console
                    Debug.Log("Object name: " + hit.transform.name);
                   
                    //add a new vertex to the line renderer
                    lineRenderer.SetVertexCount(++nPoints);
                    //set the position of the next vertex at the line renderer to be the same as the hit point
                    lineRenderer.SetPosition(i+1,hit.point);
                }
            }
        }
    }
}

hello,

The code in a if statement need to return a boolean. So the “if” can work.
RaycastHit2D is not a boolean, you can’t say if the statement is true or false.

the exemple in documentation is a little different.

        RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up);
        if (hit.collider != null)  // computer can say if that's true or false
        {
        }
1 Like

That’s a simple syntax error. Move your Physics2DHit declarations to somewhere outside the if statement. You can’t declare a new variable inside an if statement’s parantheses. You can* however test for non-zero values with any object. Goes back to the old days of C, where any non-zero value (including negative) was considered true.

1 Like

This fixed the error, thankyou (: