- Home /
`Random' does not contain a definition for `Range'?
Hello,
I'm a beginner at coding and try some basics. One of this is the Random class to let me show how it works.
using UnityEngine;
using System.Collections;
public class Random : MonoBehaviour
{
private float delTime = 1.0f;
private float currentTime;
void Start()
{
currentTime = Time.time;
}
void Update ()
{
if(Time.time - currentTime > delTime)
{
print(Random.Range(0, 1));
}
}
}
If I try to compile it in monodevelope this error appear:
Assets/Scripts/ScriptsLearning/Random.cs(18,38): error CS0117: Random' does not contain a definition for
Range'
But in the API description Range is part of the Random class.
What mistake I made?
Answer by MaT227 · Jul 27, 2014 at 01:33 PM
Here you are overriding the Unity Random class with your own Random class.
That's why there is a conflict between yours and the Unity Random class. And the error message means that "Ok you want to override the Unity class then you need to override all the methods and properties and your are forgetting the Range method".
If you want to create your own random class, you need to create it under a namespace.
Try this:
using UnityEngine;
using System.Collections;
namespace MyNamespace
{
public class Random : MonoBehaviour
{
private float delTime = 1.0f;
private float currentTime;
void Start()
{
currentTime = Time.time;
}
void Update ()
{
if(Time.time - currentTime > delTime)
{
print(UnityEngine.Random.Range(0, 1));
}
}
}
}
Ah, now I see my mistake. I didn't realize that I override the unity class but now I see. Thank you.
Answer by PubuCM · Feb 01, 2016 at 05:12 PM
Got the Same Problem after I created A Script called "Random".It affected my other scripts .I deleted it and Problem Solved.
Just put it in an another namespace, and only use that namespace when you need it.
Your answer
Follow this Question
Related Questions
(C#) Problem with animations not playing 1 Answer
I am having a parsing error in a C# pause script, can anyone help? 1 Answer
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System. 0 Answers
Distribute terrain in zones 3 Answers
SetActive() Object reference not set to an instance of an object. 1 Answer