[英]Unity gives “Type or namespace definition, or end-of-file expected” error
Getting two errors here, but I'm pretty sure they're dependent upon each other:这里有两个错误,但我很确定它们是相互依赖的:
Assets\Course Library\Scripts\PlayerController.cs(9,16): error CS1513:
}
expected Assets\Course Library\Scripts\PlayerController.cs(9,16): 错误 CS1513:}
预期Assets\Course Library\Scripts\PlayerController.cs(42,1): error CS1022: Type or namespace definition, or end-of-file expected Assets\Course Library\Scripts\PlayerController.cs(42,1): error CS1022: Type or namespace definition, or end-of-file expected
It doesn't really make any sense to me.这对我来说真的没有任何意义。 I'm certain I formatted it correctly, and that the syntax is correct.我确定我的格式正确,并且语法正确。 Anytime I make any changes to the line of code where the errors are, it only throws more errors at me.每当我对错误所在的代码行进行任何更改时,它只会向我抛出更多错误。
I thought there was a possibility that there was only one mistake somewhere in the code that causing a compound reaction, but I couldn't find anything wrong.我认为有可能代码中只有一个错误导致复合反应,但我找不到任何错误。
Could someone with more knowledge about Unity please explain to me what it is I'm doing wrong?对Unity有更多了解的人可以向我解释我做错了什么吗?
Code:代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
void Start()
{
public float tank = GameObject.Find("tank").transform.localPosition.x;
}
public float horizontalInput;
public float speed = 5;
public float xRange = 20;
public float zRange = 30;
public float zRange2 = -8;
public float turnSpeed = 20;
public float forwardInput;
void Update()
{
if (transform.position.x < -xRange){
transform.position = new Vector3(-xRange, transform.position.y, transform.position.z);
}
if (transform.position.x > xRange){
transform.position = new Vector3(xRange, transform.position.y, transform.position.z);
}
if (transform.position.z < zRange2){
transform.position = new Vector3(transform.position.x, transform.position.y, zRange2);
}
if (transform.position.z > zRange){
transform.position = new Vector3(transform.position.x, transform.position.y, zRange);
}
horizontalInput = Input.GetAxis("Horizontal");
forwardInput = Input.GetAxis("Vertical");
transform.Translate(Vector3.forward * Time.deltaTime * speed * forwardInput);
transform.Rotate(Vector3.up, turnSpeed * horizontalInput * Time.deltaTime);
}
}
In general: It is very unlikely that a compiler is broken in a way that it throws this kind of exception;)一般来说:编译器不太可能以抛出这种异常的方式被破坏;)
In Start
you do在Start
你做
public float tank = GameObject.Find("tank").transform.localPosition.x;
You can not define a public
field within the Start
method (or any method in general to be exact).您不能在Start
方法(确切地说是一般的任何方法)中定义public
字段。
Thus, before the public
keyword the compiler expects a }
to close the Start
method.因此,在public
关键字之前,编译器需要一个}
来关闭Start
方法。 The rest is just follow-up errors caused by this first one. rest 只是第一个导致的后续错误。
The compiler will try to continue and "assume" the }
was where it expects it thus your class PlayerController
would already be closed with the next }
after public float tank...
so the next error appears when hitting public float horizontalInput
since it would require a class
/ struct
around it.编译器将尝试继续并“假设” }
是它所期望的位置,因此您的 class PlayerController
已经在public float tank...
之后关闭了下一个}
......所以在点击public float horizontalInput
输入时会出现下一个错误,因为它需要围绕它的class
/ struct
。 Therefore you a因此你一个
Type or namespace definition, or end-of-file expected类型或命名空间定义,或预期的文件结尾
You probably rather wanted你可能更想要
public float tank;
private void Start()
{
tank = GameObject.Find("tank").transform.localPosition.x;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.