Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Unity says "PlayerMovement.TakeInput()" is a method, which is not valid in the given context.
This is my code

C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed;
    private Vector2 direction;

    void Update()
    {
        TakeInput();
        Move();
    }

    private void Move()
    {
        transform.Translate(direction * speed * Time.deltaTime);
    }

    private void TakeInput()
    {
        direction = Vector2.zero;
        if (TakeInput.GetKey(KeyCode.W))
        {
            direction += Vector2.up;

        }
        if (TakeInput.GetKey(KeyCode.A))
        {
            direction += Vector2.left;

        }
        if (TakeInput.GetKey(KeyCode.S))
        {
            direction += Vector2.down;

        }
        if (TakeInput.GetKey(KeyCode.D))
        {
            direction += Vector2.right;

        }
    }
}


What I have tried:

Ive tried looking up ways to solve this problem.
Posted
Updated 13-Jun-19 4:48am
v3

C++
if (TakeInput.GetKey(KeyCode.W))

You cannot use TakeInput as an object reference, as you have previously declared it as a method of this class. I presume that your code should be just:
C++
if (GetKey(KeyCode.W))
 
Share this answer
 
Comments
[no name] 18-Jun-19 8:20am    
thnx, this option works for me well
Is TakeInput overriding some MonoBehavior::TakeInput routine? If so, maybe something like: if (MonoBehavior::TakeInput(GetKey(KeyCode.W)) ..., or something like that is what you are after.
 
Share this answer
 
The function is a member function, but you use it as static function. Use it as a member on a object.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900