1

Unity is throwing out this compiler error when I run my code

"Assets\Stealth.cs(25,13): error CS0120: An object reference is required for the non-static field, method, or property 'RaycastHit2D.collider'"

I've tried declaring the RaycastHit2D variable in multiple different ways, and all throw out a different error.

public float rotationSpeed;
public float distance;

private void Start()
{
    Physics2D.queriesStartInColliders = false;
}


void Update()
{
    transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);

    if(RaycastHit2D.collider.CompareTag("Player")){
        Destroy(RaycastHit2D.collider.gameObject);
    }
    RaycastHit2D RaycastHit = Physics2D.Raycast(transform.position, transform.right, distance);
    if (RaycastHit2D.collider != null){
        Debug.DrawLine(transform.position, RaycastHit2D.point, Color.red);
    } else {
        Debug.DrawLine(transform.position, transform.position + transform.right * distance, Color.green);
    }
}

}

It should cause a gameObject I have in the scene to destroy the player when the ray comes in contact with the player's 2D collider, and this isn't happening.

Karel Tamayo
  • 3,690
  • 2
  • 24
  • 30
  • 4
    `RaycastHit2D` is the class, not an object. The class itself does not have a collider. Only the object the class generates holds a collider. – DetectivePikachu Oct 25 '19 at 17:52
  • What line is throwing the error? –  Oct 25 '19 at 17:53
  • @Amy it says right in the second paragraph of the post what line is throwing the error. The line where he is accessing `'RaycastHit2D.collider`. Both of the calls to this should be throwing the same error. – DetectivePikachu Oct 25 '19 at 17:53
  • @DetectivePikachu Thanks, I misread that part of the error message somehow. –  Oct 25 '19 at 17:55
  • See also https://docs.unity3d.com/ScriptReference/RaycastHit2D-collider.html for correct usage. – 3Dave Oct 25 '19 at 17:57

1 Answers1

4

Compiler error CS0120 is saying that you are trying to access an instance member as though it were a static member.

RaycastHit2D is a class name, so the expression RaycastHit2D.collider is trying to access a static member named collider. However, collider is an instance property, so you need an instance of RaycastHit2D instead.

Your variable RaycastHit holds an instance of this class, so you may have intended to write RaycastHit.collider instead, like this:

void Update()
{
    transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);

    RaycastHit2D RaycastHit = Physics2D.Raycast(transform.position, transform.right, distance);
    if (RaycastHit.collider != null){

        // This had to be moved below where RaycastHit is assigned,
        // and after verifying that collider is not null
        if(RaycastHit.collider.CompareTag("Player")){
            Destroy(RaycastHit.collider.gameObject);
        }

        Debug.DrawLine(transform.position, RaycastHit.point, Color.red);
    } else {
        Debug.DrawLine(transform.position, transform.position + transform.right * distance, Color.green);
    }
}
Stephen Jennings
  • 12,494
  • 5
  • 47
  • 66
  • This fixed the compiler error, but now I have the error "object reference not set to the instance of an object", what does this mean? Thank you. – HARRISON YOUNGMAN Oct 25 '19 at 18:07
  • @HARRISONYOUNGMAN https://stackoverflow.com/questions/779091/what-does-object-reference-not-set-to-an-instance-of-an-object-mean –  Oct 25 '19 at 18:14