Build error "A new Expression requires() or [] after type"

Asked

Viewed 154 times

1

I’m creating a video game based on a youtube... And in this video there is a link to the site with the codes, I copied the codes and pasted it exactly as it was to later modify and do my way... But there are some mistakes in Unity that I can’t fix!

Error occurs in class code Dinheiro:

using UnityEngine;
using System.Collections;

public class Dinheiro : MonoBehaviour {

public Vector2 velocity = new Vector2(0, -4);
public Vector3 velocity3D = new Vector3(0, -4, 0);
public float range = 4;

// Use this for initialization
void Start () {

    //rigidbody2D.velocity = velocity;
    transform.position = new Vector3(transform.position.x – range * Random.value, transform.position.y , transform.position.z);

}



// Update is called once per frame
void Update () {
    transform.position += velocity3D * Time.deltaTime;
}

}

The error message is A new expression requires () or [] after type and the compiler indicates line 14, where there is the command transform.position = new Vector3(..., as shown below:

Codigos

  • Apparently there’s nothing wrong with line 14 of the class Dinheiro (with the command transform.position = new Vector3...). Are you sure that this is the code being used by your Unity project?

  • Yes, I’m sure! I’ve seen nothing wrong, I’ve wondered too!

  • Okay. If you zip up your project and put it somewhere for me to download, I can try it out here on my Unity. Ah, and next time you open a question try to be more objective and complete. You could have just put the class code Dinheiro, indicated the line where potentially the problem is (because the compiler tells you so). Also avoid loose headings (like the one you used). So you probably wouldn’t have received the negative votes you received. :)

  • 1

    Okay, thank you so much for the tips! I put it in Dropbox: https://www.dropbox.com/s/h93lj2j700t74ui/Unity2D.zip?dl=0

1 answer

2


After performing some tests, the problem seems to be related to the characters used in the code.

As you said yourself, you copied the code from a web page and pasted it directly into the Unity editor. Once that was done, the text was pasted as formatted on page (that is, using Unicode-encoded characters instead of ANSI). For example, a longer hyphen instead of the subtraction signal is what caused the pointed error in the question.

This type of error is more easily observable in the strings where the pasted code is this (which are other errors that you would get if you fixed this first):

InvokeRepeating(“CreateObstacle”, 1f, 1.5f); // em Generate.cs

instead of being like this (using quotes normal, as typed directly via keyboard):

InvokeRepeating("CreateObstacle", 1f, 1.5f);

This "problem" probably stems from the Unity editor’s feature of pasting formatted text (type RTF). I don’t know if this may or may not be considered a tool bug, but it probably causes these kind of difficulties frequently. :)

  • 1

    Thank you!! It worked!! : D Vc is 10

  • 1

    Nothing. : ) Questions of Unity are very welcome (at least for me! hehehe). But don’t forget to work on the question a little bit more next time, okay? It’s not just an ear tug (and don’t get me wrong, please), but it’s that well-constructed questions certainly make it a bit easier for authors to get better answers (and faster).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.