Unexpected symbol `new'

In following the video 19 Declaring your own classes I followed the code that you have and I am getting the following error in unity but not in Visual Studio. I know where the error is at but I am wondering why I get the error in my project but Ben did not. I will say I am using unity 2017 and I am using Visual Studio 2017 and I do understand this could be the case but want to make sure I am not missing anything :slight_smile:

I know this line is the cause but I am not sure why:
location new Vector2(2, 3);

Unity Error:
image

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Printscreen : MonoBehaviour {

    public Vector2 location;
    public Vector2 homeLocation;

	// Use this for initialization
	void Start () {
        Vector2 pathToHome = homeLocation - location;
        print("Welcome to Go Home!");
        print("A game were you need to find your way back");
        print("Path to Home " + pathToHome);
        print("Distance to homr: " + pathToHome.magnitude);

        if (location == homeLocation)
        {
            print("I am at home!");
        }
    }
	
	// Update is called once per frame
	void Update () {
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            print("Left Key Pressed");
        }
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            location new Vector2(2, 3);
            print("Right Key Pressed");
        }
    }
}

As I was typing I figured out the solution but wanted to share the problem just in case anyone else ran into this issue.

I forgot one simple thing and that was the = sign
location = new Vector2(2, 3);

Here is my complete code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Printscreen : MonoBehaviour {

    public Vector2 location;
    public Vector2 homeLocation;

	// Use this for initialization
	void Start () {
        Vector2 pathToHome = homeLocation - location;
        print("Welcome to Go Home!");
        print("A game were you need to find your way back");
        print("Path to Home " + pathToHome);
        print("Distance to homr: " + pathToHome.magnitude);

        if (location == homeLocation)
        {
            print("I am at home!");
        }
    }
	
	// Update is called once per frame
	void Update () {
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            print("Left Key Pressed");
        }
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            location = new Vector2(2, 3);
            print("Right Key Pressed");
        }
    }
}

Privacy & Terms