CS0246 and CS8196

I’m doing a weapon pickup/throw script, and when I want to enter play mode, these errors show up:

Assets\Scripts\WeaponManager.cs(39,140): error CS8196: Reference to an implicitly-typed out variable 'hitInfo' is not permitted in the same argument list.
and:
Assets\Scripts\WeaponManager.cs(31,32): error CS0246: The type or namespace name 'List<>' could not be found (are you missing a using directive or an assembly reference?)

The script is:

using UnityEngine;

public class WeaponManager : MonoBehaviour
{
    public float pickupRange;
    public float pickupRadius;

    public int weaponLayer;

    public Transform weaponHolder;
    public Transform playerCamera;

    private bool isWeaponHold;
    private Weapon holdWeapon;

    private void Update()
    {
        if (isWeaponHold)
        {
            if (Input.GetKeyDown(KeyCode.Q))
            {
                holdWeapon.Throw(playerCamera);
                holdWeapon = null;
                isWeaponHold = false;
            }
        }
        else if (Input.GetKeyDown(KeyCode.E)) {
            var hitList = new RaycastHit[256];
            var HitNumber = Physics.CapsuleCastNonAlloc(playerCamera.position, playerCamera.position + playerCamera.forward * pickupRange, pickupRadius, playerCamera.forward, hitList);

            var realList = new List<RaycastHit>();
            for (var i = 0; i < HitNumber; i++) {
                var hit = hitList[i];
                if (hit.transform.gameObject.layer != weaponLayer) return;
                if (hit.point == Vector3.zero)
                {
                    realList.Add(hit);
                }
                else if (Physics.Raycast(playerCamera.position, hit.point - playerCamera.position, out var hitInfo, hit.distance + 0.1f && hitInfo.transform == hit.transform))
                {
                    realList.Add(hit);
                }
            }

            if (realList.Count == 0) return;

            realList.Sort((hit1, hit2) => {
                var dist1 = GetDistanceTo(hit1);
                var dist2 = GetDistanceTo(hit2);
                return Mathf.Abs(dist1 - dist2) < 0.001f ? 0 : dist1 < dist2 ? -1 : 1;
            });

            isWeaponHold = true;
            holdWeapon = realList[0].transform.GetComponent<Weapon>;
            holdWeapon.Pickup(weaponHolder);
        }
    }

    private float GetDistanceTo(RaycastHit hit)
    {
        return Vector3.Distance(playerCamera.position, hit.point == Vector3.zero ? hit.transform.position : hit.point);
    }
}

I don’t know why is it wrong, and if someone knows the answer I would be really grateful.

For your first issue, I think it is because you are initiating a var hitInfo and then trying to use it in the same statement - when you try to access its transform data. This might be fixed by initiating it with the HitInfo type as opposed to var but otherwise you’ll have to find another workaround.

The second issue is just because you haven’t included the using System.Collections; namespace. and actually Lists might be in System.Collections.Generic; I can’t remember off the top of my head. Hopefully this helps.

On line 39 I think your parentheses are messed up. I suspect you wanted an “if” statement that first does a raycast, and then if that’s successful also does some checks on the result. But instead, you are passing an expression containing the result of the raycast as the optional fourth parameter into the raycast, which of course doesn’t work because the result hasn’t been computed yet.

The List class is in the namespace System.Collections.Generic, so either you need to put using System.Collections.Generic; at the top of your file or you need to spell out the full name like new System.Collections.Generic.List<RaycastHit>()

I added the System.Collections.Generic, and it fixed the list error. Now I don’t know how to do the if statement, i’m bad with if’s, can you send an example please?

An example of what, exactly?

Quoting you:

There are a ton of Physics.Raycast examples here:

Caution: Physics Raycast is one of the most flexible functions out there: it can be called in many different ways. Pay close attention to the types of the arguments and all the combinations and purposes for using one or the other.

Thanks! I’ll check it out