cant attack script to prefab.

here’s the code but it cant seem to attach it an error called “script class cannot be found” appears I named the script MYPlayer and the prefab MYPlayer. the console window does show any errors but the script inspector tab still has the “no mono behaviour scripts in the file, or their names do not match the file name.”

code below:

using UnityEngine;
using UnityEngine.InputSystem;

public class Player : MonoBehaviour
{
private Controls controls;

[SerializeField] private bool moveKeyHeld;

private void Awake() => new Controls();

private void OnEnable()
{
    controls.Enable();

    controls.Player.Movement.started += OnMovement;
    controls.Player.Movement.canceled += OnMovement;

    controls.Player.Exit.performed += OnExit;

}

private void OnDisable()
{
    controls.Disable();

    controls.Player.Movement.started -= OnMovement;
    controls.Player.Movement.canceled -= OnMovement;

    controls.Player.Movement.Exit.performed -= OnExit;
}

private void OnMovement(InputAction.CallbackContext ctx)
{
    if (ctx.started)
    moveKeyHeld = true;
    else if (ctx.canceled)
    moveKeyHeld = false;
}

private void OnExit(InputAction.CallbackContext ctx)
{
    Debug.Log("Exit");
}

private void FixedUpdate()
{
    transform.postion += (Vector3)controls.Player.Movement.ReadValue<Vector2>();
    GameManager.instance.EndTurn();
}

}

1 Answer

1

You’ve named the script “MYPlayer” but the class is called “Player”, these are the two that need to match. It doesn’t matter what you name your prefab.

Hope this helps!

Just noticed that you're not setting your Controls member. In the Awake function, you create a new controls object but never assign this to your controls object. private void Awake() { controls = new Controls(); } That => operator is good for when you want to shorten a really simple function that just returns something like so: private float GetPi() { return 3.1415f; } private float GetPi() => 3.1415f; Since Awake is a void, it doesn't make much sense to use it here.