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?
\$\begingroup\$
\$\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\$– ZeusCommented Aug 6, 2019 at 16:58
Add a comment
|
2 Answers
\$\begingroup\$
\$\endgroup\$
0
You could specify the namespace explicitly:
System.Random random = new System.Random(seed);
random.Next();
\$\begingroup\$
\$\endgroup\$
3
You can use a using alias directive to give System.Random a new name as well:
using Rng = System.Random;
-
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\$ 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 whereUnityEngine.Random
is available. \$\endgroup\$ Commented Aug 5, 2019 at 22:48