Home > Mobile >  Unity Raycast Warning
Unity Raycast Warning

Time:04-09

I have a Raycasting script, which works, but I keep getting this warning: My VSCommunity Warning Image Line 5: [SerializeField] Camera camera; I don't think the warning is a major problem, but I still want to fix it.

Here is my code:

public class RaycastCheck : MonoBehaviour
{
    Camera camera;
    GameObject raycastHitGameObject;


    public GameObject GetRaycastHitGameObject()
    {
        RaycastHit hit;
        Ray ray = camera.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit))
        {
            raycastHitGameObject = hit.transform.gameObject;
        }

        return raycastHitGameObject;
    }

    private void Awake()
    {
        camera = GetComponent<Camera>();
    }
}

CodePudding user response:

This is simply stating that somewhere in your inheritance chain, a class variable named camera is already declared.

You can hide the already declared variable by using the new keyword.

new Camera camera;
  • Related