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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Need help - Script must derive from MonoBehaviour

Discussion in '2D' started by NoScopeKiej, Jul 4, 2019.

  1. NoScopeKiej

    NoScopeKiej

    Joined:
    Jul 4, 2019
    Posts:
    1
    Hey!

    So I'm a Python developer but I decided to try out some C#. I'm attempting to import this script: https://github.com/Brackeys/2D-Character-Controller into my game so my PlayerMovement.cs script can interact with it as the controller for my character. Sadly, this happens every time I try to attach the script to the player character
    upload_2019-7-4_20-5-6.png

    Not sure why, because the public class derives from MonoBehaviour. Help would be appreciated...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterController2D : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.        
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.        
    15.     }
    16. }
    17. using UnityEngine;
    18. using UnityEngine.Events;
    19.  
    20. public class CharacterController2D : MonoBehaviour
    21. {
    22.     [SerializeField] private float m_JumpForce = 400f;                          // Amount of force added when the player jumps.
    23.     [Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f;          // Amount of maxSpeed applied to crouching movement. 1 = 100%
    24.     [Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f;  // How much to smooth out the movement
    25.     [SerializeField] private bool m_AirControl = false;                         // Whether or not a player can steer while jumping;
    26.     [SerializeField] private LayerMask m_WhatIsGround;                          // A mask determining what is ground to the character
    27.     [SerializeField] private Transform m_GroundCheck;                           // A position marking where to check if the player is grounded.
    28.     [SerializeField] private Transform m_CeilingCheck;                          // A position marking where to check for ceilings
    29.     [SerializeField] private Collider2D m_CrouchDisableCollider;                // A collider that will be disabled when crouching
    30.  
    31.     const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
    32.     private bool m_Grounded;            // Whether or not the player is grounded.
    33.     const float k_CeilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up
    34.     private Rigidbody2D m_Rigidbody2D;
    35.     private bool m_FacingRight = true;  // For determining which way the player is currently facing.
    36.     private Vector3 m_Velocity = Vector3.zero;
    37.  
    38.     [Header("Events")]
    39.     [Space]
    40.  
    41.     public UnityEvent OnLandEvent;
    42.  
    43.     [System.Serializable]
    44.     public class BoolEvent : UnityEvent<bool> { }
    45.  
    46.     public BoolEvent OnCrouchEvent;
    47.     private bool m_wasCrouching = false;
    48.  
    49.     private void Awake()
    50.     {
    51.         m_Rigidbody2D = GetComponent<Rigidbody2D>();
    52.  
    53.         if (OnLandEvent == null)
    54.             OnLandEvent = new UnityEvent();
    55.  
    56.         if (OnCrouchEvent == null)
    57.             OnCrouchEvent = new BoolEvent();
    58.     }
    59.  
    60.     private void FixedUpdate()
    61.     {
    62.         bool wasGrounded = m_Grounded;
    63.         m_Grounded = false;
    64.  
    65.         // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
    66.         // This can be done using layers instead but Sample Assets will not overwrite your project settings.
    67.         Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
    68.         for (int i = 0; i < colliders.Length; i++)
    69.         {
    70.             if (colliders[i].gameObject != gameObject)
    71.             {
    72.                 m_Grounded = true;
    73.                 if (!wasGrounded)
    74.                     OnLandEvent.Invoke();
    75.             }
    76.         }
    77.     }
    78.  
    79.  
    80.     public void Move(float move, bool crouch, bool jump)
    81.     {
    82.         // If crouching, check to see if the character can stand up
    83.         if (!crouch)
    84.         {
    85.             // If the character has a ceiling preventing them from standing up, keep them crouching
    86.             if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
    87.             {
    88.                 crouch = true;
    89.             }
    90.         }
    91.  
    92.         //only control the player if grounded or airControl is turned on
    93.         if (m_Grounded || m_AirControl)
    94.         {
    95.  
    96.             // If crouching
    97.             if (crouch)
    98.             {
    99.                 if (!m_wasCrouching)
    100.                 {
    101.                     m_wasCrouching = true;
    102.                     OnCrouchEvent.Invoke(true);
    103.                 }
    104.  
    105.                 // Reduce the speed by the crouchSpeed multiplier
    106.                 move *= m_CrouchSpeed;
    107.  
    108.                 // Disable one of the colliders when crouching
    109.                 if (m_CrouchDisableCollider != null)
    110.                     m_CrouchDisableCollider.enabled = false;
    111.             }
    112.             else
    113.             {
    114.                 // Enable the collider when not crouching
    115.                 if (m_CrouchDisableCollider != null)
    116.                     m_CrouchDisableCollider.enabled = true;
    117.  
    118.                 if (m_wasCrouching)
    119.                 {
    120.                     m_wasCrouching = false;
    121.                     OnCrouchEvent.Invoke(false);
    122.                 }
    123.             }
    124.  
    125.             // Move the character by finding the target velocity
    126.             Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
    127.             // And then smoothing it out and applying it to the character
    128.             m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
    129.  
    130.             // If the input is moving the player right and the player is facing left...
    131.             if (move > 0 && !m_FacingRight)
    132.             {
    133.                 // ... flip the player.
    134.                 Flip();
    135.             }
    136.             // Otherwise if the input is moving the player left and the player is facing right...
    137.             else if (move < 0 && m_FacingRight)
    138.             {
    139.                 // ... flip the player.
    140.                 Flip();
    141.             }
    142.         }
    143.         // If the player should jump...
    144.         if (m_Grounded && jump)
    145.         {
    146.             // Add a vertical force to the player.
    147.             m_Grounded = false;
    148.             m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
    149.         }
    150.     }
    151.  
    152.  
    153.     private void Flip()
    154.     {
    155.         // Switch the way the player is labelled as facing.
    156.         m_FacingRight = !m_FacingRight;
    157.  
    158.         // Multiply the player's x local scale by -1.
    159.         Vector3 theScale = transform.localScale;
    160.         theScale.x *= -1;
    161.         transform.localScale = theScale;
    162.     }
    163. }
    164.  
     
  2. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    356
    CharacterController2D : MonoBehaviour


    Is defined twice? Remove the empty class and match the class and file name.

    CharacterController2D.cs
     
  3. TigerDawn

    TigerDawn

    Joined:
    Aug 4, 2018
    Posts:
    21
    Try and deleting everything in your target file, and copying the stuff from the link you provided. You have some copy and paste issues.