Search Unity

Noob Question: Random Number Ambiguous reference

Discussion in 'Scripting' started by CoachCarter9, Apr 8, 2009.

  1. CoachCarter9

    CoachCarter9

    Joined:
    Mar 17, 2009
    Posts:
    40
    So I've been fiddling with trying to make a random number in JavaScript. Using this code:

    Code (csharp):
    1.  
    2. import System;
    3. var date = DateTime.Now;
    4. var shadowPercent;
    5.  
    6. function Update () {
    7.     shadowPercent = Random.Range(0,100);
    8.     Debug.Log("shadowRange= " + shadowPercent);
    9. }
    10.  
    Attaching that to an object works perfectly fine.

    If I then try to use this same script in a different JavaScript file it gives me:

    BCE0004: Ambiguous reference 'Random': System.Random, UnityEngine.Random.

    using:

    Code (csharp):
    1.  
    2. var shadowPercent;
    3. function TimeIntensity(){
    4.     var currentSeconds = date.TimeOfDay.Ticks / 10000000;
    5.    
    6.     if(currentSeconds >= 21600  currentSeconds <= 32400){
    7.         GameLight.intensity = .02;
    8.     }
    9.     if(currentSeconds > 32400  currentSeconds <= 57600){
    10.         GameLight.intensity = .4;
    11.     }
    12.     if(currentSeconds > 54000  currentSeconds <= 68400){
    13.         GameLight.intensity = .2;
    14.     }
    15. }
    16. function Update(){
    17.     TimeIntensity();
    18.     shadowPercent = Random.Range(0,100);
    19. }
    Can anyone tell me why it is giving me this error?

    EDIT: It seems to be coming from import System. When I remove this it doesn't give me the ambiguous name anymore.

    Does anyone have any solutions on how to use both of them.
     
  2. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    As you noted, when you import the System namespace, you are also in effect removing the need to specify the namespace when calling anything contained in the System namespace. I won't go into the details as that can be confusing.

    Since System has a class named Random and UnityEngine has a class named Random, the compiler cannot tell which one you want to use.

    Normally without importing System, you would have to call System.Random(...) to gain access. Once you import System you can use its classes without the namespace prefix.

    To tell the compiler which one you want to use, prefix it with the namespace name. So if you want to use Unity's, call UnityEngine.Random(...), if you want .Net's call System.Random(...).

    -Jeremy
     
  3. pixelRainbow

    pixelRainbow

    Joined:
    Jan 26, 2010
    Posts:
    3
    Jeremy, your explanation helped me, too. Thanks! :)

    I have a script where I load and save XML data and import System; is right in of course used. In my case, I bumped into the same error message when trying to use Resources.Load() later on in the code.