Search Unity

  1. Unity 2020.2 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Bug An object reference is required for the non-static field, method, or property

Discussion in 'Scripting' started by JustYourFriendlyNeighbourhoodSlug, Nov 27, 2020.

  1. JustYourFriendlyNeighbourhoodSlug

    JustYourFriendlyNeighbourhoodSlug

    Joined:
    Aug 24, 2020
    Posts:
    15
    Code (CSharp):
    1.     public int Player1Char;
    2.  
    3.     void FixedUpdate()
    4.     {
    5.         if(Randomize == true)
    6.         {
    7.             Player1Char = System.Random.Next(1, 6); //error here
    8.             UnityEngine.Debug.Log(Player1Char);
    9.         }
    10.     }
    Using this code I'mm receiving an error saying:
    Assets\Scripts\Gameplay.cs(41,18): error CS0120: An object reference is required for the non-static field, method, or property 'Random.Next(int, int)'

    It's happening on the labelled line, any help? I've never had to work with random numbers before now :/.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    4,841
    You want UnityEngine.Random, not System.Random
     
  3. JustYourFriendlyNeighbourhoodSlug

    JustYourFriendlyNeighbourhoodSlug

    Joined:
    Aug 24, 2020
    Posts:
    15
    so i've made that change but now the error says:
    Assets\Scripts\Gameplay.cs(41,37): error CS0117: 'Random' does not contain a definition for 'Next'

    if i remove the next it says something along the lines of random not being usable as a method
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    11,133
    Go look at the API for UnityEngine.Random(). It is not the same as System.Random.
     
unityunity