- Home /
random issue
Random' is an ambiguous reference between
UnityEngine.Random' and `System.Random'
I am attempting to create a variable with a value between 1 and 20. I set it to private. int _variable=Random.Range(1,20); but when I put it into a gui label, it spits out the above error. I have: using UnityEngine; using System.Collections; using System; At the top. What am I missing????????? Thanks!
It may be a copy/paste issue, but why is there a . after private?
Answer by sebas77 · Mar 29, 2012 at 05:12 PM
well just explicity choose which one you want to use and write
int _variable=System.Random.Range(1,20); OR
int _variable=UnityEngine.Random.Range(1,20);
honestly I do not know which one you should use, but I guess is the second one (just checked, it is the second one ;) )
Thanks! I wonder, why the change? It's not mentioned anywhere else. These kinds of things make it very hard to learn this stuff.
There is no change, it's because you're importing both the UnityEngine and System namespaces, both of which have Random. So you have to tell it which one you're referring to. If you're not using anything from System, don't import the namespace; it's not a standard thing to import in Unity.
I was using System so I could utilize the enum list in another script.
No, enums don't need the system namespace, but the Enum class. However if you use the Enum class just at a few spots it's better to use the full classname (System.Enum) ins$$anonymous$$d of importing the whole System namespace.
When you import multiple namespaces there's always a chance that you end up with ambiguous class names. Only import those namespaces you need the most.
Answer by Martian-Games · Oct 19, 2015 at 03:05 AM
just add this line:
using Random=UnityEngine.Random;
Answer by EliezerYT · Apr 03, 2020 at 12:05 AM
esto es viejo jaja pero mi conclusion que encontre esque System Y UnityEngine tiene diferentes funcion cada una del Random... por lo que si usas la classe inicial de UnityEngine y System va a chocar y pide que cual exactamente quieres usar por lo que antes se deberia poner el UnityEngine.Random.Range(x,y) y asi
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Randomly Select String from Array 0 Answers
Random.Range, GameObjects and Length 1 Answer
Randomizer code not working. 1 Answer