V
V
Vyacheslav Tanchuk2021-05-02 13:28:49
Android
Vyacheslav Tanchuk, 2021-05-02 13:28:49

How to make a random number generator in unity?

I wanted to make a random number generator in Unity, but there are errors in the code.

The code:

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

public class RandManager : MonoBehaviour
{

    public float Result;
    [SerializeField] InputField One;
    [SerializeField] InputField Two;
    [SerializeField] Text ResultText;

    public void RandGenerator()
    {
        Result = Random.Range(One, Two);
        ResultText.text = "" + Result;
    }

}


Errors in this line:
Result = Random.Range(One, Two);

Writes:
Argument 1: Cannot convert from "UnityEngine.UI.InputField" to "float"
Argument 2: Cannot convert from "UnityEngine.UI.InputField" to "float"

Highlights red words:
One, Two

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2021-05-02
@GavriKos

Learn seasharp first, don't mess with the unit.
Random.Range expects 2 float, and you shove some kind of InputField to it. But what if the InputField is empty, a string, a smiley, or something else?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question