Search Unity

CS0103: The name 'movement' does not exist in the current context

Discussion in 'Getting Started' started by whistlinghook2000, May 18, 2020.

Thread Status:
Not open for further replies.
  1. whistlinghook2000

    whistlinghook2000

    Joined:
    May 17, 2020
    Posts:
    7
    I keep getting this error and I'm not sure why or how to solve it :
    Assets\scripts\PlayerController.cs(35,21): error CS0103: The name 'movement' does not exist in the current context
    I also have the warning: Assets\scripts\PlayerController.cs(11,23): warning CS0649: Field 'PlayerController.rb' is never assigned to, and will always have its default value null
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float speed;
    8.     public float jumpPower = 1f;
    9.  
    10.     private Vector3 moveDirection;
    11.     private Rigidbody rb;
    12.     private int count;
    13.     private bool jump;
    14.     private bool isGrounded;
    15.     private Vector3 jumpDirection;
    16.  
    17.  
    18.     void Update()
    19.     {
    20.         float moveHorizontal = Input.GetAxis("Horizontal");
    21.         float moveVertical = Input.GetAxis("Vertical");
    22.  
    23.         jump = Input.GetButton("Jump");
    24.  
    25.         moveDirection = new Vector3(moveHorizontal, 0.0f, moveVertical);
    26.     }
    27.  
    28.     void FixedUpdate()
    29.     {
    30.         Move();
    31.         Jump();
    32.     }
    33.     void Move()
    34.     {
    35.         rb.AddForce(movement * speed);
    36.     }
    37.     void Jump()
    38.     {
    39.         LayerMask layer = 1 << gameObject.layer;
    40.         layer = ~layer;
    41.         isGrounded = Physics.CheckSphere(transform.position, 1f, layer);
    42.  
    43.         if (jump && isGrounded)
    44.         {
    45.             rb.AddForce(jumpDirection * jumpPower, ForceMode.Impulse);
    46.         }
    47.     }
    48.     void OnCollisionEnter(Collision collision)
    49.     {
    50.         jumpDirection = collision.contacts[0].normal;
    51.     }
    52.  
    53.  
    54.  
    55.     void OnTriggerEnter(Collider other)
    56.     {
    57.         if (other.gameObject.CompareTag("Pick Up"))
    58.         {
    59.             other.gameObject.SetActive(false);
    60.  
    61.         }
    62.     }
    63. }
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You haven't declared a "movement" variable anywhere.

    Your Rigidbody reference hasn't been assigned.
    Either assign it from the inspector or assign it using
    GetComponent
    in either
    Awake
    or
    Start
    .
     
    whistlinghook2000 likes this.
  3. whistlinghook2000

    whistlinghook2000

    Joined:
    May 17, 2020
    Posts:
    7
    I tried using GetComponent and Start in this way and i really doubt im doing this right
    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.  
    4. }
    5. public class GetComponent : Player.rb
    6. {
    7.     void start();
    8.     public float speed;
    9.     public float jumpPower = 1f;
    10.     private Vector3 moveDirection;
    11.     private Rigidbody rb;
    12.     private int count;
    13.     private bool jump;
    14.     private bool isGrounded;
    15.     private Vector3 jumpDirection;
    16.  
    also how would i declare a movement variable?
     
    Last edited: May 18, 2020
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
  5. whistlinghook2000

    whistlinghook2000

    Joined:
    May 17, 2020
    Posts:
    7
  6. wilmercapellan34242

    wilmercapellan34242

    Joined:
    Dec 6, 2020
    Posts:
    1
    ayudenme

    usando el sistema;
    utilizando System.Collections.Generic;
    usando UnityEngine;
    espacio de nombres UnityStandardAssets.Characters.ThirdPerson
    {
    [RequireComponent (typeof (UnityEngine.AI.NavMeshAgent))]
    [RequireComponent (typeof (ThirdPersonCharacter))]
    clase pública AICharacterControl: MonoBehaviour
    {
    public UnityEngine.AI.NavMeshAgent agent {get; conjunto privado; } // el agente navmesh requerido para la búsqueda de ruta
    carácter público ThirdPersonCharacter {get; conjunto privado; } // el personaje que estamos controlando
    público Transform target; // objetivo al que apuntar
    // Use esto para la inicialización
    inicio vacío privado ()
    {
    // obtener los componentes en el objeto que necesitamos (no debe ser nulo debido a que requiere un componente, por lo que no es necesario verificarlo)
    agent = GetComponentInChildren <UnityEngine.AI.NavMeshAgent> ();
    personaje = GetComponent <Tercer personaje de persona> ();
    birdAgent = GetComponent <UnityEngine.AI.NavMeshAgent> ();
    birdAnimator = GetComponent <Animator> ();
    agent.updateRotation = falso;
    agent.updatePosition = true;
    }
    // La actualización se llama una vez por fotograma
    Actualización de vacío privado ()
    {
    si (objetivo! = nulo)
    {
    agent.SetDestination (target.position);


    // usa los valores para mover el personaje
    character.Move (agent.desiredVelocity, false, false);
    }
    más
    {
    // Todavía necesitamos llamar a la función de movimiento del personaje, pero enviamos una entrada con cero como parámetro de movimiento.
    character.Move (Vector3.zero, false, false);
    }
    }
    public void SetTarget (Transformar destino)
    {
    this.target = target;
    }
    }
    }
     
  7. FnapyTeam

    FnapyTeam

    Joined:
    May 21, 2021
    Posts:
    1
    can you help me with this: Assets\scripts\PlayerMovement.cs(16,13): error CS0103: The name 'input' does not exist in the current context

    this is my script any help?
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour {
    public float movementSpeed;
    public Rigidbody2D rb;

    public float jumpForce = 20f;

    float mx;

    private void Update() {
    mx = Input.GetAxisRaw("Horizontal");

    if (input.GetButtonDown("jump")) {
    Jump();
    }
    }

    private void FixedUpdate() {
    Vector2 movement = new Vector2(mx * movementSpeed, rb.velocity.y);

    rb.velocity = movement;
    }

    void Jump() {
    Vector2 movement = new Vector2(rb.velocity.x, jumpForce);

    rb.velocity = movement;
    }
    }
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Capitalization matters in C#. You wrote Input correctly just 2 lines above the error.
     
  9. kingbeaver3

    kingbeaver3

    Joined:
    Jun 11, 2021
    Posts:
    1
    you can just put in the public are public float movement
     
  10. Viaxer

    Viaxer

    Joined:
    Jun 23, 2021
    Posts:
    1
    hello any help with my script here?
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AI;

    public class Moving : MonoBehaviour
    {

    Ray lastRay;

    // Update is called once per frame
    void Update()
    {
    if (input.GetMouseButtonDown(0))
    {
    lastRay = Camera.main.ScreenPointRay(input.mousePosition);
    }
    Debug.DrawRay(lastRay.origin, lastRay.direction * 100);


    }
    }
     
  11. Deleted User

    Deleted User

    Guest

    I got the error the first person had and idk what to do...
     
  12. Samix7

    Samix7

    Joined:
    Sep 28, 2021
    Posts:
    1
    the i in imput in Imput.GetMouseButtonDown has to be big so its = Imput
     
  13. J-Noise

    J-Noise

    Joined:
    Jan 4, 2014
    Posts:
    1
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovementScript : MonoBehaviour
    {
    public CharacterController controller;

    public float speed = 7f;
    public float gravity = -9.81f;
    public float jumpHeight = 2f;
    public float runningSpeed = 10f;
    private Rigidbody rb;
    private movementDirection;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;
    bool isGrounded;

    // Update is called once per frame
    void Update()
    {
    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

    if(isGrounded && velocity.y < 0)
    {
    velocity.y = -2f;
    }

    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    Vector3 move = transform.right * x + transform.forward * z;

    controller.Move(move * speed * Time.deltaTime);

    if(Input.GetButtonDown("Jump") && isGrounded)
    {
    velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    }

    velocity.y += gravity * Time.deltaTime;

    controller.Move(velocity * Time.deltaTime);

    if (Input.GetKey(KeyCode.LeftShift)) {
    rb.AddForce(movementDirection * runningSpeed, ForceMode.Force);
    }
    else {
    rb.AddForce(movementDirection * speed, ForceMode.Force);
    }
    }
    }
    I am getting the same problem. Been on this for a couple of hours now and getting really frustrated with it.
     
  14. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    You spelt Input wrong :D
     
  15. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    How many more times do you newbies want telling about using CODE tags when displaying code?!
     
Thread Status:
Not open for further replies.