#Use of unassigned variable
1 messages · Page 1 of 1 (latest)
Because it might be unassigned 
You're not
Setting it to null initially works but why it wont work without it
What if no boats are within Distance
The error was because there was a possible route the code could take where no objects are within Distance, and so it would be "unassigned" because NearestBoat was never set to any object
You need to consider every possible path the code can take
Aight
And is setting the variable to Infinity a good way to do this?
I mean I could set it to the distance from the first boat but this is a nice shorcut
Absolutely, float.PositiveInfinity or float.MaxValue are both fine. You want it to be initialised to a high value so that all objects are potentially considered
But don't use Mathf.Infinity
Why?
Mathf.Infinity is an absolutely pointless value that Unity wrote for.. literally no reason.
It is identical to float.PositiveInfinity - that's how it's defined to be. The difference is, float.PositiveInfinity is standard C#/.NET, and will work everywhere. Mathf.Infinity is strictly a Unity thing
so it's good habit to use float.PositiveInfinity, because you will know what that value is wherever you take your C# skills