`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?

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));
    }
  }
  }
}

Got the Same Problem after I created A Script called “Random”.It affected my other scripts .I deleted it and Problem Solved.