Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

CS0246 and CS8196

Discussion in 'Scripting' started by Polud, May 25, 2020.

  1. Polud

    Polud

    Joined:
    Aug 8, 2017
    Posts:
    8
    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:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class WeaponManager : MonoBehaviour
    4. {
    5.     public float pickupRange;
    6.     public float pickupRadius;
    7.  
    8.     public int weaponLayer;
    9.  
    10.     public Transform weaponHolder;
    11.     public Transform playerCamera;
    12.  
    13.     private bool isWeaponHold;
    14.     private Weapon holdWeapon;
    15.  
    16.     private void Update()
    17.     {
    18.         if (isWeaponHold)
    19.         {
    20.             if (Input.GetKeyDown(KeyCode.Q))
    21.             {
    22.                 holdWeapon.Throw(playerCamera);
    23.                 holdWeapon = null;
    24.                 isWeaponHold = false;
    25.             }
    26.         }
    27.         else if (Input.GetKeyDown(KeyCode.E)) {
    28.             var hitList = new RaycastHit[256];
    29.             var HitNumber = Physics.CapsuleCastNonAlloc(playerCamera.position, playerCamera.position + playerCamera.forward * pickupRange, pickupRadius, playerCamera.forward, hitList);
    30.  
    31.             var realList = new List<RaycastHit>();
    32.             for (var i = 0; i < HitNumber; i++) {
    33.                 var hit = hitList[i];
    34.                 if (hit.transform.gameObject.layer != weaponLayer) return;
    35.                 if (hit.point == Vector3.zero)
    36.                 {
    37.                     realList.Add(hit);
    38.                 }
    39.                 else if (Physics.Raycast(playerCamera.position, hit.point - playerCamera.position, out var hitInfo, hit.distance + 0.1f && hitInfo.transform == hit.transform))
    40.                 {
    41.                     realList.Add(hit);
    42.                 }
    43.             }
    44.  
    45.             if (realList.Count == 0) return;
    46.  
    47.             realList.Sort((hit1, hit2) => {
    48.                 var dist1 = GetDistanceTo(hit1);
    49.                 var dist2 = GetDistanceTo(hit2);
    50.                 return Mathf.Abs(dist1 - dist2) < 0.001f ? 0 : dist1 < dist2 ? -1 : 1;
    51.             });
    52.  
    53.             isWeaponHold = true;
    54.             holdWeapon = realList[0].transform.GetComponent<Weapon>;
    55.             holdWeapon.Pickup(weaponHolder);
    56.         }
    57.     }
    58.  
    59.     private float GetDistanceTo(RaycastHit hit)
    60.     {
    61.         return Vector3.Distance(playerCamera.position, hit.point == Vector3.zero ? hit.transform.position : hit.point);
    62.     }
    63. }
    64.  
    I don't know why is it wrong, and if someone knows the answer I would be really grateful.
     
  2. elliot_eserin

    elliot_eserin

    Joined:
    Jan 5, 2020
    Posts:
    38
    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.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    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<T> 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>()
     
  4. Polud

    Polud

    Joined:
    Aug 8, 2017
    Posts:
    8
    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?
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    An example of what, exactly?
     
  6. Polud

    Polud

    Joined:
    Aug 8, 2017
    Posts:
    8
    Quoting you:
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    There are a ton of Physics.Raycast examples here:

    https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

    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.
     
  8. Polud

    Polud

    Joined:
    Aug 8, 2017
    Posts:
    8
    Thanks! I'll check it out