#I don't want to polute the chat

1 messages · Page 1 of 1 (latest)

solar harbor
#

Separate thread for discussion created

#

This is isolated from the main chat

hybrid belfry
#

oh nice thank you

#

I have two scenes :
Game
Home

My pause menu is in game. When I play I don't have error in my console, also when I pause the game and resume. But when I go back to the home page and I play the errors appear.
When I lauch my game on the home scene and lauch the game I have no error but when i go back to the home page and re-lauch the game the errors appear

solar harbor
#

Paste the PlayerMovement class in there: https://gdl.space/
Click the "Save" button top-right and copy the URL it gives you here

#

Do not delete or add lines, so it doesn't offset the line numbers

#

Remember, the issue is on line 59 of that script.

hybrid belfry
solar harbor
#

Paste the URL it gave you in this conversation

hybrid belfry
solar harbor
#

Seems like your playerTransform variable lost its value at some point, so you cannot modify its .localScale

#

You should have controls.Disable(); when this script is getting destroyed to avoid still detecting inputs when it's not needed anymore, and you should unsubscribe from controls.Land.Move.performed as well

#

I'm pretty sure you're getting the error because the function on lines 22-25 is still getting executed

hybrid belfry
#

Should I use an other method to move and flip my player ?

solar harbor
#

With your current code, you can't. Lambda expressions => cannot be unsubscribed from. You need to switch to a full method to be able to do that

#
void Start()
{
    controls.Land.Move.performed += HandleMove; // notice the absence of parentheses after the method name
}

void HandleMove(InputActions.CallbackContext ctx)
{
    // code here...
}
#

And to unsubscribe, you use -= instead

#

In void OnDestroy() which is a method Unity executes when the object this script is on is getting destroyed

hybrid belfry
#

like that ?

  private void Awake()
    {
        controls = new PlayerControls();
        controls.Enable();
        
    }

    private void Start()
    {
        playerTransform = transform;
        controls.Land.Move.performed += HandleMove;
    }

    public void HandleMove(UnityEngine.InputSystem.InputAction.CallbackContext context)
    {
        controls.Land.Move.performed += ctx -= {
            direction = ctx.ReadValue<float>();
            FlipCharacterDirection();
        };

        controls.Land.Jump.performed += ctx -= Jump();
    }
solar harbor
#

No

hybrid belfry
#

I am not sure i understand how to use =-

solar harbor
#

You already subscribe to performed in Start(), so you don't do it again inside HandleMove

#

In HandleMove there should just be the two instructions:

direction = ctx.ReadValue<float>();
FlipCharacterDirection();
#

And then you make an OnDestroy() method in which you do controls.Land.Move.performed -= HandleMove; to unsubscribe

#

With += you tell it "notify me when this happens by executing HandleMove".
With -= you tell it "stop notifying me"

hybrid belfry
#

these two line make the direction and jump ?

solar harbor
#

Yes you'll have to do the same for the jump. No because the jump is detected via controls.Land.Jump.performed

#

These are two different events, so you need two different handlers. The easiest would be to modify your Jump method so it also takes a InputAction.CallbackContext context parameter, and subscribe/unsubscribe like you're doing now

hybrid belfry
#

ok ok i'm going to try

#

You are so talented

#

thank you so much

solar harbor
#

Keep that in mind for the Input System, you pretty much always need to unsubscribe from the event when you don't need them anymore.
My go-to way of doing it is to subscribe in Awake and unsubscribe in OnDestroy, and if you need to disable the object/script and not have it receive input events, then call controls.Enable() in void OnEnable() and controls.Disable() in void OnDisable() and you should be set!

hybrid belfry
#

I'll keep taht in mind

#

thanks