Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# compiler error: Assets\Scripts\CamraMotor.cs(13,41): error CS1526: A new expression requires (), [], or {} after type

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading