1

Looking to generate a random float but random.range doesn't work since "you need to specify System.Random() or UnityEngine.Random()". When using System.Random as shown it says "non-invocable member "Random" cannot be used as a method", even though it should be. UnityEngine.Random() is a weird device which messes up other random values I'm looking at doing later on. Getting real sick of unity, program makes simple coding so difficult.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System;
    using UnityEngine.UI;


    //using System.Random;

    public class idk : MonoBehaviour
    {
    public float john = 0f;
    public float epic_gaming = 0f; //time variable
    public int gaming;

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        epic_gaming += 1;
        if (epic_gaming == 120)
        {
            gaming = System.Random(1,10);
            Debug.Log(gaming);
            epic_gaming = 0;
        }
    }
}

Indentations might look weird in the code above so that it fits but I assure you it's fine. Thanks in advance.

this isn't the same as the other question posted below, as that question is just about C# (I even read through it before asking mine) while my problem stemmed from Unity's errors/incompatibility with those methods.

Argon
  • 49
  • 1
  • 3
  • 2
    `System.Random` is a class, you need to instantiate it. – dymanoid Aug 27 '19 at 11:21
  • thanks, but how do I do that? – Argon Aug 27 '19 at 11:24
  • https://docs.unity3d.com/ScriptReference/Random.Range.html – derHugo Aug 27 '19 at 11:27
  • I tried: Random rnd = new System.Random(); int rndnumber = rnd.Next(1, 10); but it gave the system.random/unityengine.random error again for the beginning of the line ("Random rnd"). Using the docs.unity3d webpage above doesn't work either since it's outdated and gives the exact same error. – Argon Aug 27 '19 at 11:28
  • See my answer on how to solve namespace ambiguity – Menyus Aug 27 '19 at 11:33
  • Possible duplicate of [Random Numbers in Unity3D?](https://stackoverflow.com/questions/28161754/random-numbers-in-unity3d) – Ruzihm Aug 27 '19 at 16:11

1 Answers1

4
gaming = Random.Range(1, 10)

Random.Range returns an int if you use it with integers.

Doc: Unity Random.Range()

Also add this to the header: using static UnityEngine.Random;

this should solve this: you need to specify System.Random() or UnityEngine.Random()

or use the full namespace like this:

gaming = UnityEngine.Random.Range(1,10);
Menyus
  • 6,633
  • 4
  • 16
  • 36
  • 1
    It also works with floats if you specify float paramters. – FCin Aug 27 '19 at 11:29
  • I specified "public float gaming;" and then used this: "gaming = UnityEngine.Random.Range(1.1,10.9);" but apparently it only takes ints. How do I change that? – Argon Aug 27 '19 at 11:37
  • 2
    Specify that you are using a float value like this `gaming = UnityEngine.Random.Range(1.1f,10.9f);` – Menyus Aug 27 '19 at 11:39
  • 2
    ahhhhhhhh I can't believe I didn't see that, thank you so much! – Argon Aug 27 '19 at 11:40
  • @Argon Mark the answer accepted if you can still can, the guys marked it as duplicate but actually it was like 2 duplicates :d – Menyus Aug 27 '19 at 11:45