Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use C# .NET 6 PriorityQueue in Unity

I'm trying to use PriorityQueue in Unity with C#. The documentation says that it's supported in .NET 6 in namespace System.Collections.Generic.

I've tried:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class Test : Monobehaviour
{
    void Start()
    {
        var queue = new PriorityQueue<int, int>();
    }
}

But an error is thrown back:

The type or namespace name 'PriorityQueue<,>' could not be found (are
you missing a using directive or an assembly reference?)
[Assembly-CSharp]

I've checked the .NET version within VS Code: enter image description here

Why doesn't it work in Unity?

like image 798
silverfox Avatar asked May 07 '26 18:05

silverfox


1 Answers

The version reported by dotnet is completely unrelated to Unity's C#/.NET version (at least until Unity finally migrates from Mono and then moves to CoreCLR etc.). While the language supported is almost equal to C# 9.0, the actual library that is used is a loose superset of what Mono provides, and actually is roughly equal to .NET Framework 4.8/.NET Standard 2.0 ATM; since neither of those has PriorityQueue, it's simply not available. You can either:

  • wait for the transition to .NET 6+ happen (be aware it may take a couple of years - it's still an ongoing effort in 2025, with CoreCLR support being announced as "to come" for ~Unity 7 betas :),
  • use a 3rd party implementation,
  • implement it yourself,
  • copy-paste the code from the official source (minor tweaks will be required for it to work) or
  • use this direct port from official C# lib (single-file drop-in, ported by me, MIT licensed open source, no string attached).
like image 90
user213769 Avatar answered May 09 '26 06:05

user213769



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!