繁体   English   中英

统一的 C# 中的错误

[英]Errors in C# for unity

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);
            }
        }
    }

我没有在代码中找到错误。

我收到以下错误:

Assets\scripts\PlayerController.cs(35,13):错误 CS8801:在此上下文中无法使用在顶级语句中声明的局部变量或局部函数“canJump”。

Assets\scripts\PlayerController.cs(38,35):错误 CS8801:在此上下文中无法使用在顶级语句中声明的局部变量或局部函数“canJump”。

Assets\scripts\PlayerController.cs(45,13):错误 CS8801:在此上下文中无法使用在顶级语句中声明的局部变量或局部函数“canJump”。

Assets\scripts\PlayerController.cs(38,35):错误 CS0165:使用未分配的局部变量“canJump”

你的变量在错误的位置

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);
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM