MathF.Infinity error

Error message: error CS0117: ‘MathF’ does not contain a definition for ‘Infinity’

My script:

  void UpdateTarget()
    {
        GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag);

        float shortestDistance = MathF.Infinity;
        GameObject nearestEnemy = null;

        foreach (GameObject enemy in enemies)
        {
            float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
            if (distanceToEnemy < shortestDistance)
            {
                shortestDistance = distanceToEnemy;
                nearestEnemy = enemy;
            }
        }
    }

MathF.Infinity does not seem to wanna work, thoughts?

It’s Mathf not MathF.

Wow, that’s quite embarrassing.
I appreciate the response and it being so quick, I can keep going now.
Thankyou!
(And my apologies for the stupid question)

This sorta stuff SHOULD be handled by your IDE’s code completion (aka, intellisense).

Perhaps yours isn’t configured correctly?

This may help you with intellisense and possibly other Visual Studio integration problems:

Sometimes the fix is as simple as doing Assets → Open C# Project from Unity. Other times it requires more.

Other times it requires you also nuke the userprefs and .vsconfig and other crufty low-value high-hassle files that Visual Studio tends to slowly damage over time, then try the above trick.

Barring all that, move on to other ideas:

Also, try update the package inside of Unity: Window → Package Manager → Search for Visual Studio Editor → Press the Update button

Depending on flavor and version of Visual Studio, it may also have an installation step that you perform within the actual Visual Studio. This step seems finicky at best and may require multiple openings of VS before it comes up.

Update: The VSCode extension has been deprecated and abandoned:

Update: the VSCode integration is back… maybe!?

There may be a community fork available that is receiving updates.

Also, this: No suggestions in Vscode

Recently (July 2023) I worked on a Windows11 system that required a Microsoft component to be installed from within Visual Studio before it would work properly with all the OTHER software installed under Unity. I have no documentation on that process as I have only seen it once and it surprised me as well.

Also, for future reference:

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

  • Do not TALK about code without posting it.
  • Do NOT post unformatted code.
  • Do NOT retype code. Use copy/paste properly using code tags.
  • Do NOT post screenshots of code.
  • Do NOT post photographs of code.
  • ONLY post the relevant code, and then refer to it in your discussion.

It actually WAS being handled by the IDE. There is another package in the core C# library called MathF, which indeed has no Infinity. Its purpose is very similar but not identical to the Unity package called Mathf.

I have changed it to the correct code tag instead of screenshot.
My apologies and thank you for letting me know