9
\$\begingroup\$

I have a problem in Unity (C#) where I would like to create a random number. I wanted to use System.Random (reference: using System) but Unity complains that it's ambiguous to their own UnityEngine.Random. I can not specify the reference (using System.Random) as random is not a namespace. How do I specify that I want to use the system random and not the Unity one?

\$\endgroup\$
  • \$\begingroup\$ It seems to me that the title of the question doesn't really reflect the question. The easiest way to do what the tile says is simply use Unity's RNG, which may be generally good advice anyway. Of course if you're not using anything from the UnityEngine namespace (unlikely) you could just omit that using statement. \$\endgroup\$ – Zeus Aug 6 at 16:58
31
\$\begingroup\$

You could specify the namespace explicitly:

System.Random random = new System.Random(seed);
random.Next();
\$\endgroup\$
25
\$\begingroup\$

You can use a using alias directive to give System.Random a new name as well:

using Rng = System.Random;
\$\endgroup\$
  • 5
    \$\begingroup\$ using Random = System.Random; is fine too \$\endgroup\$ – trollingchar Aug 4 at 17:13
  • 8
    \$\begingroup\$ using LiterallyAnyOldString = System.Random; is also fine, you can use anything you want, the same rules apply as for variables \$\endgroup\$ – MindSwipe Aug 5 at 9:23
  • 6
    \$\begingroup\$ @MindSwipe I think trollingchar's point was that Random is fine and unambiguous, even in contexts where UnityEngine.Random is available. \$\endgroup\$ – Alexander - Reinstate Monica Aug 5 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.