Why am I getting these errors for my collision animation?

I’m trying to combine make it such that when an object collides, the animation plays. I’m getting these 2 errors, but I have no idea why they’re errors?

here is my code

I’m pretty sure you were trying to write this:

public class Collider : MonoBehaviour
{
    public GameObject item;
    public Animation anim;

    private void Start()
    {
        anim = GetComponent<Animation>();
    }

    private void OnCollisionEnter(Collision col)
    {
        anim.Play();
    }
}

Several things to note:

  1. A method cannot be defined within another method (At least not in C#). Therefore, you cannot define the Start method inside your OnCollisionEnter method.
  2. A foreach statement must have a curley brackets scope after it, where you tell the program what to do in every iteration.
  3. Variables that are defined inside methods (A.K.A “Local variables”) don’t and cannot have access modifiers (e.g public, private, etc.).

Due to the above reasons, you faced these errors.
Good luck.