The type or namespace name 'InputValue' could not be found

Lazc_coding 41 Reputation points
2022-09-19T19:10:05.6+00:00

Thank you cooldadtx for helping me with the last question, I really don't understand most of this stuff.

I have another question tho. For my actual code, I got the error:
Assets\Player_Controller.cs(61,17): error CS0246: The type or namespace name 'InputValue' could not be found (are you missing a using directive or an assembly reference?)
Assets\Player_Controller.cs(6,19): error CS0234: The type or namespace name 'InputSystem' does not exist in the namespace 'UnityEngine' (are you missing an assembly reference?)

How would I make a directive or an assembly reference for "InputValue" and "InputSystem" or is there something else that I have to do?

My code:

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Threading;
using UnityEngine;
using UnityEngine.InputSystem;

//Takes and handles input and movement for a player character
public class NewBehaviourScript : MonoBehaviour
{

public float moveSpeed = 1f;  
public float collisionOffset = 0.05f;  
public ContactFilter2D movementFilter;  

Vector2 movementInput;  

Rigidbody2D rb;  

List<RaycastHit2D> castCollision = new List<RaycastHit2D>();  

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

private void FixedUpdate() {  
    // If movement input is not 0, try to move  
    if (movementInput != Vector2.ZERO){  
        bool success = TryMove(movementInput);  

        if (!success) {  
            success = TryMove(new Vector2(movementInput.x, 0));  

            if (!success) {  
                success = TryMove(new Vector2(0, movementInput.y));  
            }  
        }  
    }  
}  

private bool TryMove(Vector2 direction) {  
    // Check for potential collisions  
    int count = rb.Cast(  
        direction, // X and Y valuses between -1 and 1 that represent the direction from the body to look for collisions  
        movementFilter, // The settings that determine where a collision can occur on such as layer to collide with  
        castCollisions, // List of collisions to store teh found collisions into after the Cast is finished  
        moveSpeed * Time.fixedDeltaTime + collisionOffset); // The amount to cast equal to teh movement plus and offset  

    if (count == 0) {  
        rb.MovePosition(rb.position + direction * moveSpeed * Time.fixedDeltaTime);  
        return true;  
    } else {  
        return false;  
    }  
}  



void OnMove(InputValue movementValue)  
{  
    movementInput = movementValue.Get<Vector2>();  
}  

}

Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2022-09-19T19:52:56.68+00:00

    Please tag your questions with csharp instead of Windows so it is categorized correctly.

    The error you're getting means you are using an identifier InputValue and the compiler cannot figure out what it is. Given that it is expecting a type or namespace that means you're using it in a context where it doesn't exist. I assume the actual line is the using line. When posting code please ensure you wrap all the code in the Code Sample editor and identify which line(s) the compiler is complaining about. The line #s from the compiler won't line up with what is in the editor.

    Jumping over to the Unity documentation it appears they have a namespace UnityEngine.InputSystem but your code isn't referencing that. You need to follow the directions given by Unity here. Once you follow those steps then the new namespace should be available and you should get past the compiler errors.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.