Search Unity

error

Discussion in 'Scripting' started by ThePerkoGames, Jan 7, 2022.

  1. ThePerkoGames

    ThePerkoGames

    Joined:
    Apr 1, 2021
    Posts:
    1
    im getting this weird error, 'Random' is an ambiguous reference between 'UnityEngine.Random' and 'System.Random', and ive looked it up, but i cant find any solution that works about how to fix this error, and i tried everything i found about this error and none of em worked. pls help.


    heres my code


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System;
    6. using System.Threading;
    7.  
    8. public class NewBehaviourScript : MonoBehaviour
    9. {
    10.              
    11.     // Start is called before the first frame update
    12.     public Text Text;
    13.     void Start()
    14.     {  
    15.        
    16.         bool rg = true;
    17.        
    18.         do
    19.         {
    20.             PlayerPrefs.SetString("Stat", "red");
    21.             Random rnd = new System.Random();
    22.             int sec  = rnd.Next(1000, 6000);
    23.             Thread.Sleep(sec);
    24.             PlayerPrefs.SetString("Stat", "green");
    25.  
    26.            
    27.         }while(rg == true);
    28.     }
    29.     void Update()
    30.     {
    31.         if(PlayerPrefs.GetString("Stat") == "red") {
    32.             Text.text = "Red Light";
    33.         }
    34.         else{
    35.             Text.text = "Green Light";
    36.         }
    37.     }
    38.  
    39.  
    40. }
    41.  
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    If two people named "Steve" were in the same room and you shouted "Steve" then it'd be an ambiguous shout because it's not clear who you wanted. It's the same with using the type "Random" when the compiler doesn't know which you mean, hence the error.

    This is because you made both "System" and "UnityEngine" available via "using" and both contain a type named "Random".

    You almost got there because you were explicit when you typed "System.Random" however look before it, you used "Random" on its own so you either need to use "var rnd =" or again using "System.Random rnd =".

    You can also alias a word like this (I prefer to do this) which would help:
    Code (CSharp):
    1. using Random = System.Random;