Help applying 2D Catch Game Tutorial to my own 2D Catch Game

I am trying to create a 2D Catch Game using the one from the tutorial as a guide, but I am having trouble with the script. In the HatController script (for controlling the hat player object) I changed hatWidth to ufoWidth but Visual Studio says: (local variable) float ufoWidth use of unassigned local variable "ufoWidth’

How do I assign it? I am new to programming and am trying to learn by making a game.

You assign to it with an assignment statement, e.g. “ufoWidth = 42;”.

Before you start mucking about with scripts, I think it’d be well worth your time to head on over to learncs.org, and work your way through the short course of tutorials there. That will give you a foundation in the basics, so you won’t trip over things like this.

Good luck, and have fun!

1 Like

If you want to assign a value to a float you’ll need to do something like this:

ufoWidth = 42.0f;

Or in the actual declaration of the variable:

float ufoWidth = 42.0f;

Use of the “f” at the end is necessary to eliminate a potential warning. It’s not a harmful one but it’s annoying.

Just a ‘technical’ note here; your advice is very good for people starting out, and the suffix can be overlooked/forgotten.
With ‘42’ it is not required:
https://docs.microsoft.com/en-us/dotnet/articles/csharp/language-reference/keywords/explicit-numeric-conversions-table

1 Like

Thanks for the heads up. Did a quick test and you’re right it wasn’t necessary. One of those habits I picked up because it complained enough times I felt it was always necessary and the one time I don’t verify it turns out not to be. :stuck_out_tongue:

1 Like