[英]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);
}
}
}
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(35,13):错误 CS8801:在此上下文中无法使用在顶级语句中声明的局部变量或局部函数“canJump”。
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(38,35):错误 CS8801:在此上下文中无法使用在顶级语句中声明的局部变量或局部函数“canJump”。
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(45,13):错误 CS8801:在此上下文中无法使用在顶级语句中声明的局部变量或局部函数“canJump”。
Assets\scripts\PlayerController.cs(38,35): error CS0165: Use of unassigned local variable 'canJump' Assets\scripts\PlayerController.cs(38,35):错误 CS0165:使用未分配的局部变量“canJump”
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);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.