General question about Visual Studio coding using the [SerializeField]

I have a general question about Visual Studio. When entering my code, specifically, when putting in [SpecializeField] and whatever variable etc. that I enter after that, as soon as I enter the semicolon, it automatically moves everything on that line except for [SpecializeField] to the next line. How can I stop that from happening? I am sure that it did not do that earlier in the course.

At some point, it started doing that and I haven’t been able to figure why or how to stop it. I have been having to manually backspace to get the line of code together on the same line. It’s a little annoying to have to keep doing that every time. Or maybe I’m being a bit too picky but if I can make things a little easier, then I would like to so things move along more smoothly.

Hi Penny,

Can you confirm, it is definitely Visual Studio you are using and not MonoDevelop?

Visual Studio has a wealth of customisation options available for how code entry/formatting will happen. You can access these from Tools / Options.

You can select Text Editor and then expand the various branches underneath;

I’ve had a quick look through myself this morning and haven’t spotted anything specific to the behaviour you are describing, but of course the option may not be labelled “Attribute” for example. What I can say is that using Visual Studio myself I am able to enter either;

[SerializeField] private string message;

or

[SerializeField]
private string message;

No auto-formatting takes place for myself, although my personal preference in the second in the examples above anyway. This is why I ask at the top of this reply whether you are definitely using Visual Studio or MonoDevelop, as they will have separate options, Visual Studio has far more and is a richer IDE in my opinion.

I did run a couple of Google searches on your behalf also, I found articles asking about this specific feature but from about 6 years ago, some even give Unity as the context and the [SerializeField] attribute as the example. At that time it certainly looks like options weren’t available, although one person did suggest pressing CTRL + Z (undo) when the formatting takes place, that seems like a better solution than you having to press backspace numerous times if nothing else.

It would take a little time, but I would be happy to provide screenshots of the options I have selected/un-selected so that you could compare to your own configuration, this my help narrow down the search to find any difference between my setup and your own. There is the ability to export/import the user settings too, but I wouldn’t to overwrite anything that you have specifically set, so checking manually may be a more careful approach.

Another option for you, if you can’t get to the bottom of it, would be to wrap all of your attributes in a Region, that way, you could make them collapse if the amount of space they take up concerns you. For example;

    #region "Squishy Attributes"
    [TextArea(10, 14)]
    [SerializeField]
    private string storyText;

    [SerializeField]
    private State[] nextStates;
    #endregion

Expand Region
image

Collapsed Region
image

Region can be useful, however, with the limited amount of code you have in any one class in this course, you’ll probably just find yourself forever opening/closing regions - it’s also a very easy way to forget you’ve already typed something out and enter it again, causing errors that, at a glance, aren’t so obvious.

I hope something from the above is of use to you, if I find anything more concrete with regards to a specific solution I will let you know. Please do double-check the IDE you are using though, as you won’t find a lot of the above necessarily in MonoDevelop.

I don’t know what MonoDevelop is. I went through and looked at all the options of the text editor like you said but I can’t figure out anything that would affect just the SerializeField formatting. It’s weird because that is the only code that I have noticed so far that does that.

Here is a screen shot of my code. I don’t know if you can tell anything from it though. Using Ctrl Z is an option I can use though. I didn’t realize I could do that. It’s not that big a deal. It was just kind of bugging me. If you happen to figure out why it does that, let me know but you don’t have to go to a lot of trouble to figure it out. I will just use the Ctrl Z for now anyway.

Thanks for your help

1 Like

Hi Penny,

MonoDevelop was an IDE that used to ship with Unity, it was functional and looked quite similar to Visual Studio, but wasn’t as feature rich. Your screenshot confirms that you are indeed using Visual Studio.

You won’t find an option that specifcally mentions “[SerializeField]” as that is a Unity term, as opposed to anything specific to Visual Studio, you may not even see anything that mentions “attributes”.

I’m glad the CTRL + Z was at least a bit of a progress for you.

If I do happen to stumble across anything I will, of course, share it with you. In the meantime please do consider the screenshot of options offer, I am happy to take screenshots of what I have here for your comparison.

Thanks Rob

Using the Ctrl Z is very helpful so I’m fine using that for now. I’m steadily progressing through the lectures and getting close to completing the Laser Defender game.

I just hope I can remember what Rick is teaching us so that I can eventually create games on my own. There is just so much to remember.

1 Like

Hi Penny,

Great news regarding your progress.

A method to really help cement the things you have learned is to try to do a little bit extra yourself on each section. Something that hasn’t been shown, but perhaps one of those “I wonder how I could make it do this?” kind of things.

You could consider all sorts of things, player lives, scores, bonuses, time limits, power-ups, adding an options scene, customisable controls, volume levels, saving to PlayerPrefs - I could go on. :slight_smile:

By taking on one or two extra features each section you will stretch yourself, invariably using some of the things you’ve just learnt and maybe a few more. I really believe this is one of the best ways to cement that knowledge.

If you arent sure of what things to add, publish your game at the end of the section and ask for feedback and ideas - you will get them :slight_smile:

1 Like

Privacy & Terms