Hello, if like me you have some error in visual studio in the line where you type only:
Random.Range…
Just add before
UnityEngine
It’ll fix the problem.
UnityEngine.Random.Range
Bye
François
Random exists in both UnityEngine
and System
. If you have something that comes from System
(you’ll likely have a using System;
at the top) and UnityEngine
(you will also have using UnityEngine
at the top) you will get this issue.
Your solution to use the fully-qualified name is valid, but you could also use an alias
using System;
using UnityEngine;
using Random = UnityEngine.Random;
Here we gave an alias called Random
that will always be the one from UnityEngine
Hello Bixarrio.
Why in the course there isn’t the problem ?
For me if I don’t put UnityEngine before Random, I this message :
‘Random’ is an ambiguous reference between ‘UnityEngine.Random’ and ‘System.Random’
In the course in the other functions like ShouldRandomDrop, GetRandomNumberOfDrops and SelectRandomItem there no need to add something else.
I use Unity 2022.3.14f1
Thank you.
François
Because Sam opted to not have the using System;
and instead fully qualify the parts that is from System
. The methods you mention are from the DropLibrary
. In there, you have a DropConfig
that needs to be Serializable
. Serializable
is an attribute in System
and Sam chose to use it like
[System.Serializable] // <- Fully-qualified
class DropConfig
When you put the using
bits at the top, the whole file knows about everything that is in the namespace you mention. If you have using UnityEngine;
the whole file knows about everything that’s in UnityEngine
; Random
, Vector2
, Vector3
, MonoBehaviour
, etc. If you add using System;
, the file now also knows about everything that’s in System
- and this also has a type called Random
. Now the compiler doesn’t know which Random
you want to use. You can tell it by:
- fully qualifying where this random is (like your original post)
int randomIndex = UnityEngine.Random.Range(0, list.Length);
, or - by using an alias (like I mentioned in my original post)
using Random = UnityEngine.Random;
. An alias is just that; an alias. It doesn’t have to be calledRandom
, you can call it whatever you want.using Fred = UnityEngine.Random;
will work, too.Random
makes more sense, though. Or - by fully qualifying the stuff from
System
(like Sam did) and then it won’t clash either
Clear, high and strong.
Thanks to have spent time to explain this ambiguous interpretation.
Have a nice day.
Take care.
François