#Line 22, motor component is missing

1 messages · Page 1 of 1 (latest)

rancid wing
#

PlayerMotor component is missing

ashen heron
#

It looks like a null referance, something is missing a referance,

tropic vine
rancid wing
#

You can enforce the component using the RequiredComponent tag

#

It is not attached to the game object

tropic vine
#

im sorry this is my first unity game idk anything

rancid wing
#

No worries

tropic vine
#

both codes are

#

where do i use RequiredComponent

ashen heron
tropic vine
#

oh alr alr

rancid wing
#

So the problem is in the OnFoot

#

Or Movement

#

One of those is not instantiated

tropic vine
#

im pretty sure i initialised both

rancid wing
#

Can you show the code for those?

tropic vine
#

yeah

rancid wing
#

I mean the Player Input

#

And the OnFoot

tropic vine
#

oh alr one sec

#

this is the player input

rancid wing
#

Sorry, my bad

tropic vine
rancid wing
#

The problem is in the input manager line 22

tropic vine
#

im sorry but what is that?

rancid wing
#

You might have this script in you project

#

In the script folder

tropic vine
#

these are the 2 scripts i have

rancid wing
#

But the name of the class is NewMonobehaviourScript

#

Try rename it to InputManager

tropic vine
#

ok one se

rancid wing
tropic vine
#

i did it

#

now its showing me this

rancid wing
#

Ok, player input is a MonoBehaviour

#

You cannot call a new one

#

You need to add it to the GO

#

And call the GetComponent

tropic vine
#

i see

tropic vine
#

wait like this?

#

using UnityEngine;
using UnityEngine.InputSystem;

public class NewMonoBehaviourScript : MonoBehaviour
{
private PlayerInput playerinput;
private PlayerInput.OnFootActions OnFoot;

private PlayerMotor motor;

// Awake is called once before the first execution of Update after the MonoBehaviour is created
void Awake()
{
    playerinput = new PlayerInput();
    OnFoot = playerinput.OnFoot;
    
    // Ensure the PlayerMotor is attached to the GameObject
    motor = GetComponent<PlayerMotor>();
    if (motor == null)
    {
        Debug.LogError("PlayerMotor is not attached to the GameObject!");
    }
}

// FixedUpdate is called once per frame
void FixedUpdate()
{
    // Ensure motor and OnFoot.Movement are not null before processing
    if (motor == null)
    {
        Debug.LogError("Motor is null, skipping movement processing.");
        return;
    }

    if (OnFoot == null || OnFoot.Movement == null)
    {
        Debug.LogError("OnFoot or Movement action is null, skipping input processing.");
        return;
    }

    // Process the movement input
    Vector2 movementInput = OnFoot.Movement.ReadValue<Vector2>();
    motor.ProcessMove(movementInput);
}

private void OnEnable()
{
    if (OnFoot != null)
    {
        OnFoot.Enable();
    }
    else
    {
        Debug.LogError("OnFoot input is null in OnEnable.");
    }
}

private void OnDisable()
{
    if (OnFoot != null)
    {
        OnFoot.Disable();
    }
    else
    {
        Debug.LogError("OnFoot input is null in OnDisable.");
    }
}

}

rancid wing
#

Nope, like you did with the PlayerMotor

tropic vine
#

oh alr

#

one sec

#

this?

#

using UnityEngine;
using UnityEngine.InputSystem;

public class NewMonoBehaviourScript : MonoBehaviour
{
private PlayerInput playerinput;
private PlayerInput.OnFootActions OnFoot;

private PlayerMotor motor;

// Awake is called once before the first execution of Update after the MonoBehaviour is created
void Awake()
{
    playerinput = new PlayerInput();
    OnFoot = playerinput.OnFoot;

    // Ensure the PlayerMotor is attached to the GameObject
    motor = GetComponent<PlayerMotor>();
    if (motor == null)
    {
        Debug.LogError("PlayerMotor is not attached to the GameObject!");
    }
}

// FixedUpdate is called once per frame
void FixedUpdate()
{
    // Check if motor and OnFoot.Movement are not null before processing
    if (motor == null)
    {
        Debug.LogError("Motor is null, skipping movement processing.");
        return;
    }

    if (OnFoot == null || OnFoot.Movement == null)
    {
        Debug.LogError("OnFoot or Movement action is null, skipping input processing.");
        return;
    }

    // Get the movement input and process it
    Vector2 movementInput = OnFoot.Movement.ReadValue<Vector2>();
    if (movementInput == null)
    {
        Debug.LogError("Movement input is null or invalid.");
        return;
    }

    motor.ProcessMove(movementInput);
}

private void OnEnable()
{
    if (OnFoot != null)
    {
        OnFoot.Enable();
    }
    else
    {
        Debug.LogError("OnFoot input is null in OnEnable.");
    }
}

private void OnDisable()
{
    if (OnFoot != null)
    {
        OnFoot.Disable();
    }
    else
    {
        Debug.LogError("OnFoot input is null in OnDisable.");
    }
}

}

rancid wing
#

Change it to GetComponent<PlayerInput>()

#

And add the component to the GO

tropic vine
#

like this?

rancid wing
#

You can get rid of the new

#

Just GetComponent

#

Ad add it to the GameObject afterwards

opal folio
#

Also that thing isn't going to be a component regardless

#

So this is all wrong

opal folio
rancid wing
#

Very confusing indeed. So no need to do a GetComponent

#

But it seems the RequiredComponent tag is needed

#

@tropic vine worth read the documentation

tropic vine
#

i have

rancid wing
#

@opal folio the example from de documentation call the get component

#

@tropic vine try adding the required component tag and call the get component and see if it works

rancid wing
opal folio
#

That particular tutorial makes it confusing

#

Because they named their input actions asset "PlayerInput"

#

It's not normally that confusing

#

Note that this is now just a completely different approach being taken

tropic vine
#

oh alr i seee

#

thanks so much