VisualStudio 2017 will break if I use square brackets

Hi all,
I am having trouble connecting Unity and Visual Studio 2017, so that the IDE suggests me the fuctions present in the UnityEngine library.

It has stopped working completely when I introduce the square brackets in: [SerializeField].
I don’t know why,but it stops correctly identifying { }, thus closing if statements or functions.

I have googled before coming here, and have done the usual installation of Visual Studio Tools for Unity 2017… not working ever.

Hi,

This sounds more like a case of syntax issues, as such the compiler cannot make any sense of your code.

If you take a look at your screenshot, you’ll see the Thrust method, for example, is missing at least 4 closing curly braces. So the compiler will currently think that you Rotate method’s code is part of the Thrust method - which is then syntactically wrong.

Can you post your whole script up here please, not as a screenshot, but via copy/paste (and format it).


See also;

Hi, thanks for the quick answer.

Just to clarify, VS2017 should know of the libraries’ functions I add to the code, right?

BlockquoteI
If you take a look at your screenshot, you’ll see the Thrust method, for example, is missing at least 4 closing curly braces.

Yes, that was me trying to get Visual Studio to behave.
The interesting information there are the products I have installed.
As for the code:

Unity only throws the error CS1525: unexpected symbol, 4 times. Once per square bracket

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

public class Rocket : MonoBehaviour
{



    Rigidbody rigidBody;
    AudioSource audiosource;

    // Use this for initialization
    void Start()
    {
        rigidBody = GetComponent<Rigidbody>();
        audiosource = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        ProcessInput();
    }

    private void ProcessInput()
    {
        Thrust();
        Rotate();
    }

    void Thrust()
    {

        if (Input.GetKey(KeyCode.Space))
        {
            //todo meter botón de superturbo
            [SerializeField] int c_Thrust = 250;
            rigidBody.AddRelativeForce(c_Thrust * Vector3.up);

            if (!audiosource.isPlaying)
            {
                audiosource.Play();
            }
            else
            {
                audiosource.Stop();
            }         
        }
    }

    void Rotate()
    {
        [SerializeField] int c_Rotate = 250;
        rigidBody.freezeRotation = true; //take manual control of rotation

        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(c_Rotate *  Time.deltaTime * Vector3.forward);
        }


        if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(c_Rotate * Time.deltaTime * Vector3.back);
        }

        rigidBody.freezeRotation = false; //resume physics control of rotation


    }
}

Thanks in advance!

1 Like

Hi,

Just to clarify, VS2017 should know of the libraries’ functions I add to the code, right?

Yes, as long as the solution/project has references to the required libraries. But if you have code that cannot compile due to syntax errors, it makes little difference. The compiler needs to be able to understand all of the code in your file.

Thanks for the code.

The issue is caused by where you are trying to serialize your fields. You can serialize members variables, but not local variables, e.g. those within a method.

Take a look at the code from the project on GitHub at this stage in the course and you will see the difference.

Hope this helps :slight_smile:


See also;

Heeeey! That was a noobie one! Thanks!
Will mark this post as solved and open another one for properly connecting unity and VS 2017.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms