Object reference required?

Hey all, I’m having two little issues with my script that I’m not sure what they mean (I’m new to Unity & C#, so please forgive my naivety).

error CS0120: An object reference is required for the non-static field, method, or property ‘Component.GetComponent()’

error CS1061: ‘BitchFaceController’ does not contain a definition for ‘GetPosition’ and no accessible extension method ‘GetPosition’ accepting a first argument of type ‘BitchFaceController’ could be found (are you missing a using directive or an assembly reference?)

Here is my spikes script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spikes : MonoBehaviour
{

    [SerializeField] private int damageAmount;

    private void OnTriggerEnter2D(Collider2D collider)
    {
        BitchFaceController player = Collider.GetComponent<BitchFaceController>();
        if (player != null)
        {
            //We hit the Player
            Vector3 knockbackDir = (player.GetPosition() - transform.position).normalized;
            player.DamageKnockback(knockbackDir, 10f, damageAmount);

        }
    }
}

And here is my player code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Rewired;




public class BitchFaceController : MonoBehaviour
{

    public int playerId = 0;
    private Player player;
    public bool useController;
    public Rigidbody2D rb;
    public GameObject arrowPrefab;

    //Animations
    public Animator topAnimator;
    public Animator bottomAnimator;
    public GameObject crossHair;

    //Movement and Aiming
    Vector2 movement;
    Vector2 aim;
    private float lastDirX;
    private float lastDirY;
    bool isAiming;
    bool endAiming;
    public float speed;
    public float arrowSpeed;  

    //LERP Dodge Roll
    public bool isDodging;
    private float fracJourney;
    public Vector2 startMarker;
    public Vector2 endMarker;
    public float dodgeSpeed;
    public float dodgeDistance;
    public float animationSpeed;

    //Combat System
    public static int health;
   


    private void Awake()
    {
        Debug.Log("PlayerId is " + playerId.ToString());
        player = ReInput.players.GetPlayer(playerId);

        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    private void Start()
    {
        rb = this.GetComponent<Rigidbody2D>();
        arrowSpeed = 3;
        health = 3;
    }

    // Update is called once per frame
    void Update()
    {
        ProcessInputs();
        AimAndShoot();
        Animate();
    }

    private void FixedUpdate()
    {
        Move();
        Dodge();
    }
   

    //Moving
    private void Move()
    {
        //transform.position = transform.position + movement * speed * Time.deltaTime;
        rb.MovePosition(rb.position + (movement * speed * Time.fixedDeltaTime));
        rb.velocity = new Vector2(movement.x, movement.y);
    }


    //Dodging
    private void Dodge()
    {
        if (!isDodging && Input.GetKey(KeyCode.Space) && (rb.velocity.x != 0 || rb.velocity.y != 0))
        {
            isDodging = true;
            fracJourney = 0;
    
            //startMarker = transform.position;
            startMarker = rb.position;
            endMarker = rb.position + movement * dodgeDistance;
            //endMarker = rb.MovePosition(rb.position + (movement * dodgeDistance));

            topAnimator.SetBool("Dodge", true);
        }

        if (isDodging)
        {
            fracJourney = fracJourney + Time.deltaTime * dodgeSpeed;
           

            if (fracJourney > 1)
            {
                fracJourney = 1;
                isDodging = false;

                topAnimator.SetBool("Dodge", false);
                //this.Archer_Bottom.enabled = true;
            }

            rb.MovePosition(Vector2.Lerp(startMarker, endMarker, fracJourney));
        }   
    }

    //Processing Inputs, Aiming, and Walk Animation

    private void ProcessInputs()
    {
        if (useController)
        {
            movement = new Vector2(Input.GetAxis("MoveHorizontal"), Input.GetAxis("MoveVertical"));
            aim = new Vector2(player.GetAxis("AimHorizontal"), player.GetAxis("AimVertical"));
            aim.Normalize();
            isAiming = player.GetButton("Fire");
            endAiming = player.GetButtonUp("Fire");
            //isDodging = player.GetButton("Dodge");
        }
        else
        {
            movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
            Vector2 mouseMovement = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
            aim = aim + mouseMovement;
            if (aim.magnitude > 1.0f)
            {
                aim.Normalize();
            }
            isAiming = Input.GetButton("Fire1");
            endAiming = Input.GetButtonUp("Fire1");
        }


        if (movement.magnitude > 1.0f)
        {
            movement.Normalize();
        }

        //Set Last Direction Parameter (for Idle Facing)
        if (Input.GetKey(KeyCode.W))
        {
            lastDirX = 0;
            lastDirY = 1;
        }
        else if (Input.GetKey(KeyCode.A))
        {
            lastDirX = -1;
            lastDirY = 0;
        }
        else if (Input.GetKey(KeyCode.S))
        {
            lastDirX = 0;
            lastDirY = -1;
        }
        else if (Input.GetKey(KeyCode.D))
        {
            lastDirX = 1;
            lastDirY = 0;
        };
   

    }




    private void Animate()
    {
        //Moving Legs
        bottomAnimator.SetFloat("MoveHorizontal", movement.x);
        bottomAnimator.SetFloat("MoveVertical", movement.y);
        bottomAnimator.SetFloat("MoveMagnitude", movement.magnitude);

        //Moving Body
        topAnimator.SetFloat("MoveHorizontal", movement.x);
        topAnimator.SetFloat("MoveVertical", movement.y);
        topAnimator.SetFloat("MoveMagnitude", movement.magnitude);

        //Aiming
        topAnimator.SetFloat("AimHorizontal", aim.x);
        topAnimator.SetFloat("AimVertical", aim.y);
        topAnimator.SetFloat("AimMagnitude", aim.magnitude);
        topAnimator.SetBool("Aim", isAiming);

        //Idle Direction
        topAnimator.SetFloat("LastMoveX", lastDirX);
        topAnimator.SetFloat("LastMoveY", lastDirY);
    }

    private void AimAndShoot()
    {
        Vector2 shootingDirection = new Vector2(aim.x, aim.y);

        if (aim.magnitude > 0.0f)
        {
            crossHair.transform.localPosition = aim * 0.4f;
            crossHair.SetActive(true);

            shootingDirection.Normalize();
            if (endAiming)
            {
                GameObject arrow = Instantiate(arrowPrefab, transform.position, Quaternion.identity);
                Arrow1 arrow1Script = arrow.GetComponent<Arrow1>();
                arrow1Script.velocity = shootingDirection * arrowSpeed;
                arrow1Script.player = gameObject;
                arrow.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(shootingDirection.y, shootingDirection.x) * Mathf.Rad2Deg);
                Destroy(arrow, 1.0f);
            }
        }
        else
        {
            crossHair.SetActive(false);
        }
    }

    public void DamageKnockback(Vector3 knockbackDir, float knockbackDistance, int damageAmount)
    {
        transform.position += knockbackDir * knockbackDistance;
        //DamageFlash();
        HeartsHealthVisual.heartsHealthSystemStatic.Damage(damageAmount);
    }

}

It’s obvious that my issue is with how I’m calling the component from BitchFaceController, but I’m just really not sure what I’m doing wrong.

The other issue? I have no idea what this means.

“void OnTriggerEnter2D(Collider2D collider)”
“BitchFaceController player = Collider.GetComponent();”
The ‘C’ in “Collider” should be lowercase to match with the Collider2D parameter in the OnTriggerEnter2D method.

For the second issue, your “BitchFaceController” class doesn’t have a method called “GetPosition()”.
You can create this method in your class, but I’m assuming you just meant to write player.transform.position instead.

1 Like

Question is already answered, but I’m just here curious what kind of game has a class named BitchFaceController lol

3 Likes

Incredibly helpful, thank you!
I really struggle with how to connect scripts/objects/components in Unity. Would you happen to know of a good lesson I can check out to learn more about this?

The good kind XD

1 Like

I think you have the right idea. In this case, you essentially did it correctly. You just confused “collider” with “Collider”.

“collider” (with a small c), is the name of the parameter passed in to your OnTriggerEnter2D function. It’s the collider of the object that enters the trigger. That object has the BitchFaceController component that you wanted, so I think that you meant to call GetComponent on controller.

“Controller” with a capitol C is a the type that controller belongs to. You can get a component from a specific object, but It does not make sense to get a component from a type of object. That is what the error messages meant, essentially.

There’s a brief description of some ways to communicate values between scripts here:
https://docs.unity3d.com/Manual/ControllingGameObjectsComponents.html

That does not describe every way to pass info, but It might help.

1 Like

Hey no that’s helpful. I’ll give it a read. Thanks!