C# compiler error: Assets\Scripts\CamraMotor.cs(13,41): error CS1526: A new expression requires (), [], or {} after type
C# compiler error: Assets\Scripts\CamraMotor.cs(13,41): error CS1526: A new expression requires (), [], or {} after type
我正在学习 unity 中的 2D 游戏教程,但我不断收到编译错误,告诉我新表达式需要 ()、[] 或 {},但是当我查看我的代码时,我找不到哪里出错了。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamraMotor : MonoBehaviour
{
public Transform lookAt;
public float boundX = 0.15f;
public float boundY = 0.05f;
private void LateUpdate()
{
Vector3 delta = new Vector3.zero;
// This is to see if we're on the inside the bounds om the X asxis
float deltaX = lookAt.position.x - transform.position.x;
if(deltaX > boundX || deltaX < -boundX)
{
if (transform.position.x < lookAt.position.x)
{
delta.x = deltaX - boundX;
}
else
{
delta.x = deltaX + boundX;
}
}
// This is to see if we're inside the bounds on the Y axis
float deltaY = lookAt.position.y - transform.position.y;
if (deltaY > boundY || deltaY < -boundY)
{
if (transform.position.y < lookAt.position.y)
{
delta.y = deltaY - boundY;
}
else
{
delta.y = deltaY + boundY;
}
}
transform.position += new Vector3(delta.x, deltaY, 0);
}
}
说可以找到错误(13,41)
我尝试重新输入整个脚本,但仍然出现同样的错误。
这一行是错误的:
Vector3 delta = new Vector3.zero;
new
关键字用于创建新对象,但该对象需要一个构造函数。
删除 new
它应该可以正常工作。
这可能是您教程中的一个错误。看起来 Zero
是一个字段而不是 class,因此无法实例化。
我正在学习 unity 中的 2D 游戏教程,但我不断收到编译错误,告诉我新表达式需要 ()、[] 或 {},但是当我查看我的代码时,我找不到哪里出错了。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamraMotor : MonoBehaviour
{
public Transform lookAt;
public float boundX = 0.15f;
public float boundY = 0.05f;
private void LateUpdate()
{
Vector3 delta = new Vector3.zero;
// This is to see if we're on the inside the bounds om the X asxis
float deltaX = lookAt.position.x - transform.position.x;
if(deltaX > boundX || deltaX < -boundX)
{
if (transform.position.x < lookAt.position.x)
{
delta.x = deltaX - boundX;
}
else
{
delta.x = deltaX + boundX;
}
}
// This is to see if we're inside the bounds on the Y axis
float deltaY = lookAt.position.y - transform.position.y;
if (deltaY > boundY || deltaY < -boundY)
{
if (transform.position.y < lookAt.position.y)
{
delta.y = deltaY - boundY;
}
else
{
delta.y = deltaY + boundY;
}
}
transform.position += new Vector3(delta.x, deltaY, 0);
}
}
说可以找到错误(13,41)
我尝试重新输入整个脚本,但仍然出现同样的错误。
这一行是错误的:
Vector3 delta = new Vector3.zero;
new
关键字用于创建新对象,但该对象需要一个构造函数。
删除 new
它应该可以正常工作。
这可能是您教程中的一个错误。看起来 Zero
是一个字段而不是 class,因此无法实例化。