[SOLVED]'Random' is an ambiguous reference

Ben’s code:

Vector2 tweak = new Vector2 (Random.Range (0f, 0.2f), Random.Range (0f, 0.2f));
print (tweak);

Returns:

'Random' is an ambiguous reference between 'UnityEngine.Random' and 'System.Random'

Again, I’m using Unity 5. This is likely why I get the error and he doesn’t.

The simplest solution is adding this line of code to the top of your script:

using Random=UnityEngine.Random;

This will clear the error and let you continue working.

2 Likes

If you change it to;

using UnityEngine;

…you will most likely avoid a load of other issues in your scripts too. This gives you access to the entire UnityEngine library with the class that references it. After that you should just be able to reference the code as Random.Range, which reads a little easier on the eye too :slight_smile:

Unity will add this line by default with any new script you create from within the IDE.

Hope this helps.

I’m already using Unity.Engine.

It forced me to add the random even still.

That’s really odd. The only reason that would typically happen, and state that it was ambiguous would be if there was something else with the same reference, for example, if you had you own class called “Random” also - as such it would know when you called .Range whether you were referring to UnityEngine or YourClass.

Going to have a quick look at this myself now - 08:11 and it’s irritating me! :smiley:

Just re-read your original post - doh! I was using my phone earlier and was half asleep - you’ve stated exactly what I just said… stoopid me… sorry…

Still going to go and try it though :slight_smile:

1 Like

Just opened up a version of BlockBreaker and I didn’t seem to add that before Random and it works? I’m guessing this was from the OnCollisionEnter2D() method in the Ball.cs script?

Mine would have been originally written in an older version of Unity but then upgraded to Unity 5 (it’s currently launching in 5.3.4) - it ran fine and doesn’t indicate any issues with Random.Range(). Here’s a screenshot where I hover over Random to display where it’s referencing:

Here’s a copy of the code from that file but I suspect it’s the same as yours…

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {

	private Paddle paddle;
	private bool hasStarted = false;
	private Vector3 paddleToBallVector;

	// Use this for initialization
	void Start () {
		paddle = GameObject.FindObjectOfType<Paddle>();
		paddleToBallVector = this.transform.position - paddle.transform.position;
	}
	
	// Update is called once per frame
	void Update () {
		if(!hasStarted){
			// lock the ball relative to the paddle
			this.transform.position = paddle.transform.position + paddleToBallVector; 
		
			// wait for mouse click to launch
			if(Input.GetMouseButtonDown(0)){
				hasStarted = true;
				this.GetComponent<Rigidbody2D>().velocity = new Vector2(2f,10f);
			}
		}
	}
	
	void OnCollisionEnter2D(Collision2D collision){
		Vector2 tweak = new Vector2(Random.Range (0f,0.2f), Random.Range (0f, 0.2f));
		// ball does not trigger sound when brick is destroyed
		// probably because of Destory function elsewhere
		if(hasStarted){
			GetComponent<AudioSource>().Play ();
			GetComponent<Rigidbody2D>().velocity += tweak;
		}
		
	}
	
	public void Reset() {
		hasStarted = false;
	}
}
1 Like

Bed time now. I’ll post my code tomorrow, as well as my game.

1 Like

No worries… guess you must be the other side of the pond from me :slight_smile:

1 Like
using UnityEngine;
using System.Collections;
using UnityEngine.Audio;
using System.Collections.Specialized;
using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using Random=UnityEngine.Random;




public class Ball : MonoBehaviour {

	private Paddle paddle;
	private bool hasStarted = false;
	private Vector3 paddleToVallVector;

	// Use this for initialization
	void Start () {
		paddle = GameObject.FindObjectOfType<Paddle>();
		paddleToVallVector = this.transform.position - paddle.transform.position;



	}
	
	// Update is called once per frame
	void Update ()
	{

		if (!hasStarted) {
		// lock the ball relative to the paddle
			this.transform.position = paddle.transform.position + paddleToVallVector;

			// wait for a mouse press to launch
			if (Input.GetMouseButtonDown (0)) {
				print ("Mouse Button Clicked, launch ball.");
				hasStarted=true;
				this.GetComponent<Rigidbody2D> ().velocity = new Vector2 (2f, 10f);

			}
		}
	}

	void OnCollisionEnter2D (Collision2D col)
	{
		Vector2 tweak = new Vector2 (Random.Range (0f, 0.2f), Random.Range (0f, 0.2f));
			if (hasStarted) {
				gameObject.GetComponent<AudioSource> ().Play ();
				GetComponent<Rigidbody2D>().velocity += tweak;
		}
	}

}

Sorry for the indents. I’m not fixing it all. :stuck_out_tongue:

As you can see, I’m already using Unity.Engine. It still forces me to use:

using Random=UnityEngine.Random;

I’m not sure why. I am about to post my game, though. It’s completed and uploading to gamebucket.io right now :smiley: :smiley: :smiley:

So, regarding the ambiguity… I can see why not - because amongst the many using directives you have here, one of them is System which obviously does have a Random class, hence the issue. So, a few thoughts…

Unless you are doing something considerably more advanced than the lecture takes you through, you don’t need most of these Using directives. What I am more interested in is how they got there, and that could simply be down to some testing/experimenting at your end, or, possibly more likely, the editor that you are using to create your scripts.

In my experience, primarily with Visual Studio, the software gives you a set of project templates that you can choose from when creating a new project (this is outside of Unity, but bear with me), these templates will create what is considered to be a useful set of files/directories for you, and in many cases will often create some basic application files, for example a web project may even provide you with login and reset password web forms.

I’m wondering if perhaps your editor has tried to be helpful and included many using directives for some kind of specific project type. In my example the only two I had were;

using UnityEngine;
using System.Collections;

…and when I look more thoroughly at my code now I can see that I was a bit lazy in that I didn’t go back and check what I actually needed. The System.Collections directive in my example is complete unnecessary as I am not using anything from that set of classes.

Visual Studio is quite kind to me on that front, I can simply hover over them and it will tell me if they are being used or not, indeed it even greys them out!

What I would suggest is removing all of the ones that you do not need, and then, as you expand the functionality of the project(s) and the code within your scripts add what you need as you need it.

I’d be fairly confident you could remove the System one which would then remove the ambiguity and subsequently the fix that you needed to put in place for it. Of course I say this without being able to see all of your code and what everything is doing, so a gentle touch would be best.

If you are not using Visual Studio then my suggestion would be to comment out one of your using directives at a time, save the script and then run the game. If everything works you can safely remove the directives you’ve commented out. If something suddenly stops working, like the music for example, you may need to remove the comment characters and leave that one in. Below is what I would guess without being able to see the whole project…

using UnityEngine;
// using System.Collections;
using UnityEngine.Audio;    // I don't have this in mine in the ball script
// using System.Collections.Specialized;
// using System;
// using System.Runtime.InteropServices;
// using System.Security.Cryptography;
// using Random=UnityEngine.Random;

Hope this helps, and with regards to the other post you made about the Cryptography directive, that should be able to go also. What would be ideal is to establish what is putting them in in the first place :slight_smile:

Privacy & Terms