- Home /
how to Save A random generated number and use it
I want to use a random generated number and save it in a variable. i tried using public float x = Random.Range(minNb, maxNb); but it doesn't work. Here is the rest of my script.
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;
public class Score : MonoBehaviour
{
public float score = 0;
public float scoreMulti = 5;
public float minNb = 100;
public float maxNb = 200;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
score += Time.deltaTime * scoreMulti;
if (score == 70)
{
//save random generated number
}
}
}
Comment
Answer by IggyZuk · Oct 16, 2020 at 03:18 PM
This should work fine. x = Random.Range(minNb, maxNb);
You just gotta make sure to define it as a member variable.
public class Score : MonoBehaviour {
float x;
void Awake() {
x = Random.Range(-100f, 100f);
}
}