Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

The script needs to derive from MonoBehaviour???

Discussion in 'Scripting' started by Dortex0n, Jan 23, 2020.

  1. Dortex0n

    Dortex0n

    Joined:
    Jan 19, 2020
    Posts:
    11
    I'm starting my adventure in Unity and I've got a problem.
    I can't add my movement script for the player:
    Unity error is:
    ,,The script needs to derive from MonoBehaviour"
    This is my script:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Playermovement : MonoBehaviour
    {

    public float moveSpeed;
    public float jumpForce;

    public KeyCode Left;
    public KeyCode Right;
    public KeyCode Jump;

    private Rigidbody2D theRB;

    public Transform Player1;
    public Transform GroundCheckPoint;

    public bool isGrounded;
    public float groundCheckRadius;
    public LayerMask WhatisGround;

    private float jumpTimerCounter;
    public float jumpTime;
    public bool isJumping;



    // Start is called before the first frame update
    void Start()
    {
    theRB = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
    isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, WhatisGround);

    if(Input.GetKey(Left))
    {
    theRB.velocity = new Vector2 (-moveSpeed, theRB.velocity.y)

    }
    else if(Input.GetKey(Right))
    {
    theRB.velocity = new Vector2 (moveSpeed, theRB.velocity.y)

    }
    else
    {
    theRB.velocity = new Vector2(0, theRB.velocity.y);
    }

    if (Input.GetKeyDown(Jump) && isGrounded)
    {
    isJumping = true
    theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);

    jumpTimerCounter = jumpTime;
    }

    if (Input.GetKey(Jump) && isJumping == true)
    {
    if (jumpTimerCounter > 0)
    {
    theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
    }

    jumpTimerCounter -= Time.deltaTime;
    } else
    {
    isJumping = false;
    }

    if (Input.GetKeyUp(Jump))
    {
    isJumping = false;
    }

    if(theRB.velocity.x < 0)
    {
    tranform.localScale = new Vector3(-1, 1, 1);
    }
    else if(theRB.velocity.x < 0)
    {
    tranform.localScale = new Vector3(1, 1, 1);
    }

    }
    }

    What is wrong?