Climb Ladder - 'myCollider2D' does not exist

Not sure why this isn’t working. I’ve looked online and can’t seem to wrap my head around it.


Assets/Scripts/PlayerMovement.cs(31,9): error CS0103: The name `myCollider2D’ does not exist in the current context

Assets/Scripts/PlayerMovement.cs(81,13): error CS0103: The name `myCollider2D’ does not exist in the current context


    void Start () {
        myAnimator = gameObject.GetComponent<Animator>();
        myRigidBody = gameObject.GetComponent<Rigidbody2D>();
        myBodyCollider2D = gameObject.GetComponent<CapsuleCollider2D>();
        myFeet = gameObject.GetComponent<PolygonCollider2D>();
        myCollider2D = gameObject.GetComponent<Collider2D>(); // error 31,9
    }  

    private void ClimbLadder()
    {
        if(!myCollider2D.IsTouchingLayers(LayerMask.GetMask("Climbing")))  { return; } // error 81,13

        float controlThrow = CrossPlatformInputManager.GetAxis("Verticle");
        Vector2 climbVelocity = new Vector2(myRigidBody.velocity.x, controlThrow * climbSpeed);
        myRigidBody.velocity = climbVelocity;

        bool playerHasVerticalSpeed = Mathf.Abs(myRigidBody.velocity.y) > Mathf.Epsilon;
        myAnimator.SetBool("Climbing", playerHasVerticalSpeed);
    }

Full Script


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityStandardAssets.CrossPlatformInput;

    public class PlayerMovement : MonoBehaviour {

    [SerializeField] float runSpeed = 5f;
    [SerializeField] float jumpSpeed = 5f;
    [SerializeField] Vector2 deathKick = new Vector2(10, 0);
    [SerializeField] float climbSpeed = 5f;

    private int jumpCount = 0;
    private int maxJumps = 2;

    private Animator myAnimator;
    // States
    bool isAlive = true;

    // Cached component references
    public Rigidbody2D myRigidBody;
    CapsuleCollider2D myBodyCollider2D;
    PolygonCollider2D myFeet;


	void Start () {
        myAnimator = gameObject.GetComponent<Animator>();
        myRigidBody = gameObject.GetComponent<Rigidbody2D>();
        myBodyCollider2D = gameObject.GetComponent<CapsuleCollider2D>();
        myFeet = gameObject.GetComponent<PolygonCollider2D>();
        myCollider2D = gameObject.GetComponent<Collider2D>();
    }

    void Update()
    {
        // if the player is not alive then turn off the ability to Run,Jump,etc.
        if (!isAlive) { return; }

        Run();
        Jump();
        FlipSprite();
        Die();
        ClimbLadder();
    }

    // Flip sprite dependant on direction
    private void FlipSprite()
    {
        bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
        if (playerHasHorizontalSpeed)
        {
            transform.localScale = new Vector2(Mathf.Sign(myRigidBody.velocity.x), 1f);
        }
    }

    //Die
    private void Die()
    {
        if (myBodyCollider2D.IsTouchingLayers(LayerMask.GetMask("Enemy", "Hazards")))
        {
            isAlive = false;
            myAnimator.SetTrigger("Dying");
            GetComponent<Rigidbody2D>().velocity = deathKick;
        }
    }

    // Run
    private void Run()
    {        
        float controlThrow = CrossPlatformInputManager.GetAxis("Horizontal");
        Vector2 playerVelocity = new Vector2(controlThrow * runSpeed, myRigidBody.velocity.y);
        myRigidBody.velocity = playerVelocity;    
        
        bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
        myAnimator.SetBool("Running", playerHasHorizontalSpeed);
    }
    
    // Climb
    private void ClimbLadder()
    {
        if(!myCollider2D.IsTouchingLayers(LayerMask.GetMask("Climbing")))  { return; }

        float controlThrow = CrossPlatformInputManager.GetAxis("Verticle");
        Vector2 climbVelocity = new Vector2(myRigidBody.velocity.x, controlThrow * climbSpeed);
        myRigidBody.velocity = climbVelocity;

        bool playerHasVerticalSpeed = Mathf.Abs(myRigidBody.velocity.y) > Mathf.Epsilon;
        myAnimator.SetBool("Climbing", playerHasVerticalSpeed);
    }

    // Jump
    private void Jump() {

        if (!myFeet.IsTouchingLayers(LayerMask.GetMask("Grounded"))) { return; }
        print("Feet on the ground");
        if (CrossPlatformInputManager.GetButtonDown("Jump"))
        {
            Vector2 jumpVelocityToAdd = new Vector2(0f, jumpSpeed);
            myRigidBody.velocity += jumpVelocityToAdd;
            print("jumping");
        }       
    }
  
}

Hi Kyle,

Looks like a naming issue. Under your cached component references you have referred to it as “myBodyCollider2D”, but then in the Start method you are trying to access it via “myCollider2D”.

Emphasises the importance of naming your variables suitably, and clearly.

Hope this helps :slight_smile:


See also;

1 Like

My hero. Thank you Rob.

1 Like

You’re are very welcome Kyle, happy to help :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms