Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Random.Range not working?

Discussion in 'Scripting' started by MaeBird, Jul 23, 2020.

  1. MaeBird

    MaeBird

    Joined:
    Jul 12, 2020
    Posts:
    3
    I just started learning C# in Unity, I have been following tutorials etc. for about 2 weeks. I've been using Visual Studio for writing the scripts. Well, when following tutorials previously, I had no problem using Random.Range. I did update my versions of Unity and Visual Studio to be up-to-date as of today, so maybe that is where my problem lies? As of today, if I type Random.Range it does not recognize it, and it only gives me the option of the function just called "Random" or it suggests something called RandomNumberGenerator. I'm not sure if I've messed something up somewhere but it does this on a new C# script, using the defaults (System; System.Collections; System.Collections.Generic; UnityEngine) and inheriting from MonoBehavior. Can anyone explain this? Thank you.

    Edited:

    Actually, nothing is working now. I am getting compiler errors from just doing a Debug.Log Hello World. I will add a screenshot.
     
    Last edited: Jul 23, 2020
  2. MaeBird

    MaeBird

    Joined:
    Jul 12, 2020
    Posts:
    3
  3. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    The issue is your code, not unity. A c# method declaration must be followed by parentheses.

    So it should be void OnDisable() { }

    The compiler is correct.

    As for the Random issue, stop using System; Both system and UnityEngine contain Random classes, the ide thinks you want to use the random class in the System namespace and not UnityEngine.

    If you really need to use the system namespace, then add using Random = UnityEngine.Random; to your script.
     
    Asli213312 and PraetorBlue like this.
  4. MaeBird

    MaeBird

    Joined:
    Jul 12, 2020
    Posts:
    3
    Ah, duh. I think I fried my brain trying to figure out the random stuff. That was a silly error. Thank you for your response. I will try getting rid of System!

    Edit: I still can't get Random.Range. Every time I try, Visual Studio automatically populates with System. I feel like this change must be from my updating VS? Any additional tips are appreciated.
     
    Last edited: Jul 23, 2020
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Put this at the top of your file:
    Code (CSharp):
    1. using UnityEngine;
    2. using Random = UnityEngine.Random;