Player Death Effect Problem

Why I am getting this error when changing the Deathblow variable as shown in screenshot??

Her is my code:-

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

public class PlayerMovement : MonoBehaviour
{
    Vector2 moveInput;
    bool isPlayerRunning;
    bool isPlayerClimbing;
    bool isTouchingLayers;
    bool isPlayerAlive = true;

    Rigidbody2D playerRigidbody;
    Animator playerAnimator;
    CapsuleCollider2D playerBodyCollider;
    BoxCollider2D playerFeetCollider;
    // SpriteRenderer playerSpriteRenderer;

    [SerializeField] float playerMoveSpeed = 2f;
    [SerializeField] float playerJumpSpeed = 5f;
    [SerializeField] float playerClimbSpeed = 3f;
    [SerializeField] Vector2 deathblow = new Vector2(4f, 4f);
    // [SerializeField] Color32 playerDeathColor;
    float playerStartingGravity;

    void Start()
    {
        playerRigidbody = GetComponent<Rigidbody2D>();
        playerAnimator = GetComponent<Animator>();
        playerBodyCollider = GetComponent<CapsuleCollider2D>();
        playerFeetCollider = GetComponent<BoxCollider2D>();
        // playerSpriteRenderer = GetComponent<SpriteRenderer>();
        playerStartingGravity = playerRigidbody.gravityScale;
    }

    void Update()
    {
        isPlayerRunning = Mathf.Abs(moveInput.x) > Mathf.Epsilon;
        isPlayerClimbing = Mathf.Abs(moveInput.y) > Mathf.Epsilon;

        if(!isPlayerAlive) { return; }

        FlipPlayer();
        Run();
        ClimbLadder();
        Die();
    }

    void OnMove(InputValue value)
    {
        moveInput = value.Get<Vector2>();
        Debug.Log("Player moving...");
    }

    void OnJump(InputValue value)
    {
        if(isPlayerAlive)
        {
            if(value.isPressed && playerFeetCollider.IsTouchingLayers(LayerMask.GetMask("Ground")))
            {
                playerRigidbody.velocity += new Vector2(0f, playerJumpSpeed);
            }
        }
    }

    void FlipPlayer()
    {
        if(isPlayerRunning)
        {
            transform.localScale = new Vector2(Mathf.Sign(moveInput.x), 1f);
        }
    }

    void Run()
    {
        playerAnimator.SetBool("isRunning", isPlayerRunning);
        Vector2 playerMoveVelocity = new Vector2(moveInput.x * playerMoveSpeed, playerRigidbody.velocity.y);
        playerRigidbody.velocity = playerMoveVelocity;
    }

    void ClimbLadder()
    {
        if(playerBodyCollider.IsTouchingLayers(LayerMask.GetMask("Climbs")))
        {
            playerAnimator.SetBool("isClimbing", isPlayerClimbing);
            playerRigidbody.gravityScale = 0;
            Vector2 playerClimbVelocity = new Vector2(playerRigidbody.velocity.x, moveInput.y * playerClimbSpeed);
            playerRigidbody.velocity = playerClimbVelocity;
        }
        else
        {
            playerAnimator.SetBool("isClimbing", false);
            playerRigidbody.gravityScale = playerStartingGravity;
        }
    }

    void Die()
    {
        if(playerBodyCollider.IsTouchingLayers(LayerMask.GetMask("Enemies", "Hazards")))
        {
            isPlayerAlive = false;
            playerAnimator.SetTrigger("Dying");
            playerRigidbody.velocity = deathblow;
            // playerSpriteRenderer.color = playerDeathColor;
        }
    }
}

Hi,

Have you already tried to restart Unity and Visual Studio Code?

Could there be a typo in your script? Double click on the error message to see if it refers to anything in your code.

I have restarted my PC.
On double clicking the error there is no response.
And the problem is not just with deathblow variable, it is there when I try to change any variable in the inspector.

I found this thread. Try to delete and recreate the game object.

Also avoid apostrophes in names (paths, scripts, files) because those “special” characters often cause issues with Unity.


See also:

There is one more problem related to editor or maybe engine.

Whwn I type something in VS Code I am not getting any suggestions, in this whole section I have manually typed every single line of code. For example if I type [SerializeField] then I have to type the whole spelling correctly.

Please follow the instruction on this website and make sure all required extensions are installed: https://code.visualstudio.com/docs/other/unity

If the issue persists, please check the console of VS Code (not Unity!). If the .NET Framework 4.7.1 (Developer Pack) is mentioned there, download and install it from the official Microsoft. Here is the link: https://dotnet.microsoft.com/download/dotnet-framework/net471

Depending on your version of Unity, it might be that you will have to install the “Visual Studio Code Editor” package in Window > Package Manager in Unity.

  1. Press Ctrl + Shift + P in VS Code.
  2. Type “OmniSharp: Select Project” and press Return.
  3. Select the solution workspace entry.

Maybe you’ll have to reboot your computer. Then launch Unity again and open one of your scripts.

I previously had .Net framework but maybe it was not developer pack but installing this developer pack solves the problem.

Thank you.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms