1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

bool canJump;

public class PlayerController : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey("left"))
        {
            gameObject.transform.Translate(-50f * Time.deltaTime, 0, 0);
        }

        if (Input.GetKey("right"))
        {
            gameObject.transform.Translate(50f * Time.deltaTime, 0, 0);
        }

        ManageJump();
    }

    void ManageJump()
    {
        if(gameObject.transform.position.y <= 0)
        {
            canJump = true;
        }

        if (Input.GetKey("up") && canJump && gameObject.transform.position.y < 10)
        {
            gameObject.transform.Translate(0, 50f * Time.deltaTime, 0);
        }

        else
        {
            canJump = false;

            if (gameObject.transform.position.y > 0)
            {
                gameObject.transform.Translate(0, -50f * Time.deltaTime, 0);
            }
        }
    }

I dont find the error in the code.

I get the following errors:

Assets\scripts\PlayerController.cs(35,13): error CS8801: Cannot use local variable or local function 'canJump' declared in a top-level statement in this context.

Assets\scripts\PlayerController.cs(38,35): error CS8801: Cannot use local variable or local function 'canJump' declared in a top-level statement in this context.

Assets\scripts\PlayerController.cs(45,13): error CS8801: Cannot use local variable or local function 'canJump' declared in a top-level statement in this context.

Assets\scripts\PlayerController.cs(38,35): error CS0165: Use of unassigned local variable 'canJump'

1
  • This question isn't about unityscript, it is about c# Commented Jun 28, 2022 at 20:08

1 Answer 1

2

your variable is in the wrong location

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


public class PlayerController : MonoBehaviour
{
    // this needs to go here
    bool canJump;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey("left"))
        {
            gameObject.transform.Translate(-50f * Time.deltaTime, 0, 0);
        }

        if (Input.GetKey("right"))
        {
            gameObject.transform.Translate(50f * Time.deltaTime, 0, 0);
        }

        ManageJump();
    }

    void ManageJump()
    {
        if(gameObject.transform.position.y <= 0)
        {
            canJump = true;
        }

        if (Input.GetKey("up") && canJump && gameObject.transform.position.y < 10)
        {
            gameObject.transform.Translate(0, 50f * Time.deltaTime, 0);
        }

        else
        {
            canJump = false;

            if (gameObject.transform.position.y > 0)
            {
                gameObject.transform.Translate(0, -50f * Time.deltaTime, 0);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

yes, i just saw it... thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.