Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

'Random is is ambiguous reference between 'Unity.Random' and 'System.Random'

Discussion in 'Editor & General Support' started by Brian-Washechek, Jun 21, 2020.

  1. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    851
    6-20-2020.png
    'Random is is ambiguous reference between 'Unity.Random' and 'System.Random' - I understand 43/61 of that. I have LITTLE idea what that means. Actually it comes in at NO idea at all. Help, please?
     
    Last edited: Jun 21, 2020
  2. There are two different Random classes, there is the UnityEngine.Random and the System.Random. You usually want to use the UnityEngine.Random class.
    If this is a tutorial, you probably missed the place where the tutorial said something about it.

    If you need the UnityEngine Random, just put this in the top of the file:
    Code (CSharp):
    1. using Random=UnityEngine.Random;
     
  3. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    851
    Code (csharp):
    1. Assets\Scripts\EnemyController.cs(76,47): error CS0103: The name 'tranform' does not exist in the current context
    is the error. I get still
    Code (csharp):
    1. using System;
    2. using System.Collections;
    3. using UnityEngine;
    4. using Random = UnityEngine.Random;
    5.  
    6. public class EnemyController : MonoBehaviour
    7. {
    8.     public float moveSpeed;
    9.  
    10.     public Vector2 startDirection;
    11.  
    12.     public bool shouldChangeDirection;
    13.     public float changeDirectionXPoint;
    14.     public Vector2 changedDirection;
    15.  
    16.     public GameObject shotToFire;
    17.     public Transform firePoint;
    18.     public float timeBetweenShots;
    19.     private float shotCounter;
    20.  
    21.     public bool canShoot;
    22.     private bool allowShooting;
    23.  
    24.     public int currentHealth;
    25.     public GameObject deathEffect;
    26.  
    27.     public int scoreValue = 100;
    28.  
    29.     public GameObject[] powerUps;
    30.     void Start()
    31.     {
    32.         shotCounter = timeBetweenShots;
    33.     }
    34.  
    35.     void Update()
    36.     {
    37.         //transform.position -= new Vector3(moveSpeed * Time.deltaTime, 0f, 0f);
    38.  
    39.         if (!shouldChangeDirection)
    40.         {
    41.             transform.position += new Vector3(startDirection.x * moveSpeed * Time.deltaTime,
    42.                                           startDirection.y * moveSpeed * Time.deltaTime,
    43.                                           0f);
    44.         }else{
    45.             if(transform.position.x > changeDirectionXPoint)
    46.             {
    47.                 transform.position += new Vector3(startDirection.x * moveSpeed * Time.deltaTime,
    48.                                                   startDirection.y * moveSpeed * Time.deltaTime,
    49.                                                   0f);
    50.             }else{
    51.                 transform.position += new Vector3(changedDirection.x * moveSpeed * Time.deltaTime,
    52.                                                   changedDirection.y * moveSpeed * Time.deltaTime,
    53.                                                   0f);
    54.             }  
    55.         }
    56.  
    57.         if (allowShooting)
    58.         {
    59.             shotCounter -= Time.deltaTime;
    60.             if (shotCounter <= 0)
    61.             {
    62.                 shotCounter = timeBetweenShots;
    63.                 Instantiate(shotToFire, firePoint.position, firePoint.rotation);
    64.             }
    65.         }
    66.     }
    67.  
    68.    public void HurtEnemy()
    69.     {
    70.         currentHealth--;
    71.         if(currentHealth <= 0)
    72.         {
    73.             GameManager.instance.AddScore(scoreValue);
    74.  
    75.             int randomPick = Random.Range(0, powerUps.Length);
    76.             Instantiate(powerUps[randomPick], tranform.position, transform.range);
    77.  
    78.             Destroy(gameObject);
    79.             Instantiate(deathEffect, transform.position, transform.rotation);
    80.         }
    81.     }
    82.  
    83.     private void OnBecameInvisible()
    84.     {
    85.         Destroy(gameObject);
    86.     }
    87.  
    88.     private void OnBecameVisible()
    89.     {
    90.         if(canShoot)
    91.         {
    92.             allowShooting = true;
    93.         }
    94.     }
    95. }
     
  4. Do you even read your error messages? Read it again.
     
  5. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    851
  6. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    851
    Assets\Scripts\EnemyController.cs(76,77): error CS1061: 'Transform' does not contain a definition for 'range' and no accessible extension method 'range' accepting a first argument of type 'Transform' could be found (are you missing a using directive or an assembly reference?)

    ...

    That then?
     
  7. No problem, it's just the majority of your posts here are like this. You get an error message, you panic or I don't know and your first instinct is to post.
    But, if you want to get better, just calm down, read through the error message, multiple times.
    Recognize where the error is:
    - which file(s)
    - what line numbers
    - approximately what columns

    Open up the file, look for the problematic line. Try to read the error message again and then read the line it is complaining about.
    In the nearby lines there are plenty of "transform" usage, so you would have immediately recognize that you just mistyped the word. Nothing serious.
    If it doesn't work for you here is another technique:
    - buy a rubber duck or any stuffed or rubber or plastic figure you like (I use a Dalek from the British TV-Show Doctor Who).
    - put it on the desk next to you, it should be in your view when you are coding.
    - when you don't know what is happening, start to explain your code to this inanimate object
    - explain what are you doing and why you're doing it and actually show to it where you're doing it

    I know it sounds funny at first, but when you actually say out loud what you're doing you realize in yourself what you're doing and it helps to understand and find problems when you're too tired to find what is the problem.
    The name of the technique is Rubber Duck Debugging: https://en.wikipedia.org/wiki/Rubber_duck_debugging

    So when you read the error message, just tell this lucky figure of yours what the error says and what line and what the line sounds like. And when you say out loud that tranform.position, you will hear it that it is wrong.
     
    Last edited by a moderator: Aug 8, 2021
    Deleted User, Ohminen and erkanuzunx like this.
  8. Your first error was mistyping the transform (you wrote tranform), you probably wanted to write transform.rotation this time? I guess. There is no range in transform as the error says. Check the tutorial, what you would have to type in there instead of the range.
     
  9. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    851
    I'm just going to work on this tomorrow. Thank you.
     
    Lurking-Ninja likes this.
  10. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    851
    6-21-2020.png
    It's the day after and I'm stumped again. I can give you any info. It's just that in this error I'm any of these.
     
  11. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    851
    Code (CSharp):
    1. using Random = UnityEngine.Random;
    I got it! You had to worry about the capitalizing!
     
    amaltash_sinha likes this.
  12. SpideyBurhan

    SpideyBurhan

    Joined:
    Oct 25, 2020
    Posts:
    2
    THANKS A LOT BRO
     
    laranthir likes this.
  13. mdrafik27

    mdrafik27

    Joined:
    Nov 28, 2020
    Posts:
    1
    thanks it works!!!
     
    laranthir likes this.
  14. Abdelfettah

    Abdelfettah

    Joined:
    May 5, 2019
    Posts:
    3
    Thank you so much
     
  15. OnlyHardProjects

    OnlyHardProjects

    Joined:
    Jul 11, 2018
    Posts:
    7
    Hi mate,
    Thank you so much.
    Simple and easy
    Before
    Code (csharp):
    1.  
    2. (27,13): error CS0104: 'Random' is an ambiguous reference between 'UnityEngine.Random' and 'System.Random'
    3. (28,13): error CS0104: 'Random' is an ambiguous reference between 'UnityEngine.Random' and 'System.Random'
    4. (29,13): error CS0104: 'Random' is an ambiguous reference between 'UnityEngine.Random' and 'System.Random'
    5.  
    After
    Code (csharp):
    1.  
    2. using Random=UnityEngine.Random;
    3.  
    0 Errors
     
  16. unity_D444D06487EA6F41188D

    unity_D444D06487EA6F41188D

    Joined:
    Nov 25, 2021
    Posts:
    1
    Didn't work for me, because I use other value types from System.
    My solution was to add:
    Code (CSharp):
    1. UnityEngine.Random.Range();
     
  17. corvinkristof

    corvinkristof

    Joined:
    Sep 22, 2020
    Posts:
    3
    I have a question for this gun script can someone help me out (Also i'm kind of new (the script was copy & pasted))

    using UnityEngine;
    using TMPro;
    using System;
    using Random = System.Random;

    /// Thanks for downloading my projectile gun script! :D
    /// Feel free to use it in any project you like!
    ///
    /// The code is fully commented but if you still have any questions
    /// don't hesitate to write a yt comment
    /// or use the #coding-problems channel of my discord server
    ///
    /// Dave
    public class ProjectileGunTutorial : MonoBehaviour
    {
    //bullet
    public GameObject bullet;

    //bullet force
    public float shootForce, upwardForce;

    //Gun stats
    public float timeBetweenShooting, spread, reloadTime, timeBetweenShots;
    public int magazineSize, bulletsPerTap;
    public bool allowButtonHold;

    int bulletsLeft, bulletsShot;

    //Recoil
    public Rigidbody playerRb;
    public float recoilForce;

    //bools
    bool shooting, readyToShoot, reloading;

    //Reference
    public Camera fpsCam;
    public Transform attackPoint;

    //Graphics
    public GameObject muzzleFlash;
    public TextMeshProUGUI ammunitionDisplay;

    //bug fixing :D
    public bool allowInvoke = true;

    private void Awake()
    {
    //make sure magazine is full
    bulletsLeft = magazineSize;
    readyToShoot = true;
    }

    private void Update()
    {
    MyInput();

    //Set ammo display, if it exists :D
    if (ammunitionDisplay != null)
    ammunitionDisplay.SetText(bulletsLeft / bulletsPerTap + " / " + magazineSize / bulletsPerTap);
    }
    private void MyInput()
    {
    //Check if allowed to hold down button and take corresponding input
    if (allowButtonHold) shooting = Input.GetKey(KeyCode.Mouse0);
    else shooting = Input.GetKeyDown(KeyCode.Mouse0);

    //Reloading
    if (Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && !reloading) Reload();
    //Reload automatically when trying to shoot without ammo
    if (readyToShoot && shooting && !reloading && bulletsLeft <= 0) Reload();

    //Shooting
    if (readyToShoot && shooting && !reloading && bulletsLeft > 0)
    {
    //Set bullets shot to 0
    bulletsShot = 0;

    Shoot();
    }
    }

    private void Shoot()
    {
    readyToShoot = false;

    //Find the exact hit position using a raycast
    Ray ray = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); //Just a ray through the middle of your current view
    RaycastHit hit;

    //check if ray hits something
    Vector3 targetPoint;
    if (Physics.Raycast(ray, out hit))
    targetPoint = hit.point;
    else
    targetPoint = ray.GetPoint(75); //Just a point far away from the player

    //Calculate direction from attackPoint to targetPoint
    Vector3 directionWithoutSpread = targetPoint - attackPoint.position;

    //Calculate spread
    float x = Random.Range(-spread, spread);
    float y = Random.Range(-spread, spread);

    //Calculate new direction with spread
    Vector3 directionWithSpread = directionWithoutSpread + new Vector3(x, y, 0); //Just add spread to last direction

    //Instantiate bullet/projectile
    GameObject currentBullet = Instantiate(bullet, attackPoint.position, Quaternion.identity); //store instantiated bullet in currentBullet
    //Rotate bullet to shoot direction
    currentBullet.transform.forward = directionWithSpread.normalized;

    //Add forces to bullet
    currentBullet.GetComponent<Rigidbody>().AddForce(directionWithSpread.normalized * shootForce, ForceMode.Impulse);
    currentBullet.GetComponent<Rigidbody>().AddForce(fpsCam.transform.up * upwardForce, ForceMode.Impulse);

    //Instantiate muzzle flash, if you have one
    if (muzzleFlash != null)
    Instantiate(muzzleFlash, attackPoint.position, Quaternion.identity);

    bulletsLeft--;
    bulletsShot++;

    //Invoke resetShot function (if not already invoked), with your timeBetweenShooting
    if (allowInvoke)
    {
    Invoke("ResetShot", timeBetweenShooting);
    allowInvoke = false;

    //Add recoil to player (should only be called once)
    playerRb.AddForce(-directionWithSpread.normalized * recoilForce, ForceMode.Impulse);
    }

    //if more than one bulletsPerTap make sure to repeat shoot function
    if (bulletsShot < bulletsPerTap && bulletsLeft > 0)
    Invoke("Shoot", timeBetweenShots);
    }
    private void ResetShot()
    {
    //Allow shooting and invoking again
    readyToShoot = true;
    allowInvoke = true;
    }

    private void Reload()
    {
    reloading = true;
    Invoke("ReloadFinished", reloadTime); //Invoke ReloadFinished function with your reloadTime as delay
    }
    private void ReloadFinished()
    {
    //Fill magazine
    bulletsLeft = magazineSize;
    reloading = false;
    }
    }
     
  18. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,573
    Please do not hijack threads like this. Create your own threads and use code-tags when posting code.
     
  19. amaltash_sinha

    amaltash_sinha

    Joined:
    Dec 6, 2022
    Posts:
    1
    Thanks for the help, Btw i was thinking that the this problem Using Random = UnityEngine.Random are in new verson only because i was using old unity and there was no such problem