Home > Enterprise >  C# compiler error: Assets\Scripts\CamraMotor.cs(13,41): error CS1526: A new expression requires ()
C# compiler error: Assets\Scripts\CamraMotor.cs(13,41): error CS1526: A new expression requires ()

Time:04-04

I am following a tutorial for a 2D game in unity, but I keep getting a compiling error telling me that a new expression requires (), [], or a {}, but when I look at my code I cant find where I went wrong.

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

it said the error can be found (13,41)

I tried retyping the whole script but I'm still getting the same error.

CodePudding user response:

This line is the error:

Vector3 delta = new Vector3.zero;

The new keyword is used to create a new object, but that object needs a constructor.

Remove the new and it should work fine.

This is likely an error in your tutorial. It looks like Zero is a field and not a class, therefore it cannot be instantiated.

  • Related