Home > Software design >  Unity can't find OnMove function of New Input System
Unity can't find OnMove function of New Input System

Time:07-21

I have been working on unity's new ınput system and I have issue.I want use OnMove function of Player Input component but I'm getting this error: MissingMethodException: PlayerMovement.OnMove Due to: Attempted to access a missing member.So my character don't move.How can I fix this ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
    [SerializeField] float runSpeed = 10f;
    Vector2 moveInput;
    Rigidbody2D myRigidbody;
    void Start()
    {
       myRigidbody = GetComponent<Rigidbody2D>();
    }
    void Update()
    {
        Run();
    }
    void OnMove(TileVania value)
    {
        moveInput = value.Get<Vector2>();
    }
    void Run()
    {
        Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, 
        myRigidbody.velocity.y);
        myRigidbody.velocity = playerVelocity;
    }
}

CodePudding user response:

What is a TileVania?

I would expect a InputAction.CallbackContext there I guess.

Sounds to me like the PlayerInput is trying to call your method via SendMessage but the signature is not correct and it should rather be

void OnMove(InputAction.CallbackContext context)
{
    moveInput = context.Get<Vector2>();
}

CodePudding user response:

You can fix this by adding the

[RequireComponent(typeof(Rigidbody2D))]

attribute to your PlayerMovement script. This will make sure that your script always has a Rigidbody2D component when it's added to a game object.

  • Related