The name 'castCollision' does not exist in the current context

Lazc_coding 41 Reputation points
2022-09-20T19:38:18.45+00:00

So I tried to run my code in the Unity software and I got this error: Assets\Scripts\Player_Controller.cs(45,13): error CS0103: The name 'castCollisions' does not exist in the current context. I thought that "castCollisions" was a natural occurring function but I guess I was wrong. I've been looking at a few videos to fix it but every one of them led me to something else. Does anyone know how to fix this error and can you please explain it to me if you can?

//Takes and handles input and movement for a player character
public class NewBehaviourScript : MonoBehaviour
{

 public float moveSpeed = 1f;  
 public float collisionOffset = 0.05f;  
 public ContactFilter2D movementFilter;  
 Vector2 movementInput;  
 Rigidbody2D rb;  
 List<RaycastHit2D> castCollision = new List<RaycastHit2D>();  
 // Start is called before the first frame update  
 void Start()  
 {  
     rb = GetComponent<Rigidbody2D>();  
 }  
 private void FixedUpdate()  
 {  
     // If movement input is not 0, try to move  
     if (movementInput != Vector2.zero)  
     {  
         bool success = TryMove(movementInput);  
         if (!success)  
         {  
             success = TryMove(new Vector2(movementInput.x, 0));  
             if (!success)  
             {  
                 success = TryMove(new Vector2(0, movementInput.y));  
             }  
         }  
     }  
 }  
 private bool TryMove(Vector2 direction)  
 {  
     // Check for potential collisions  
     int count = rb.Cast(  
         direction, // X and Y valuses between -1 and 1 that represent the direction from the body to look for collisions  
         movementFilter, // The settings that determine where a collision can occur on such as layer to collide with  
         castCollisions, // List of collisions to store teh found collisions into after the Cast is finished  
         moveSpeed * Time.fixedDeltaTime + collisionOffset); // The amount to cast equal to teh movement plus and offset  
     if (count == 0)  
     {  
         rb.MovePosition(rb.position + direction * moveSpeed * Time.fixedDeltaTime);  
         return true;  
     }  
     else  
     {  
         return false;  
     }  
 }  
 void OnMove(InputValue movementValue)  
 {  
     movementInput = movementValue.Get<Vector2>();  
 }  

}

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,090 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 118.4K Reputation points
    2022-09-20T19:44:30.937+00:00

    Try changing the name to castCollisions at line 6.

    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.