10
\$\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\$
1
  • \$\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
    Commented Aug 6, 2019 at 16:58

2 Answers 2

30
\$\begingroup\$

You could specify the namespace explicitly:

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

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

using Rng = System.Random;
\$\endgroup\$
3
  • 5
    \$\begingroup\$ using Random = System.Random; is fine too \$\endgroup\$ Commented Aug 4, 2019 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
    Commented Aug 5, 2019 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
    Commented Aug 5, 2019 at 22:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .