Raycast (Method Not Found)

So I have just started using Unity a couple weeks ago and I have been making a custom script to move units to the location where I click on the screen. I don’t see any errors in the code and when I run the game, Unity says: “MissingMethodException: Method not found: ‘UnityEngine.Plane.Raycast’.”

This doesn’t make sense to me because this method is shown in it’s documentation. Below is my basic code for the character controller. I appreciate your time in helping me solve this.


var currentSpeed;
var walkSpeed = 3.0f;
var idleSpeed = 0.0f;
var selected = false;

static var selectedUnits = [];

private var targetPosition : Vector3;

function Start() {
	targetPosition = transform.position;
}

function Update () {

	var playerPlane;
	var ray;
	var hitDist = 0.0;
	if(Input.GetKeyDown(KeyCode.Mouse0)) // left click
	{
		playerPlane = new Plane(Vector3.up, transform.position);
        ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		var clickedPosition;
       
        if (playerPlane.Raycast(ray, hitDist)) {
            clickedPosition = ray.GetPoint(hitDist);
            
			if(clickedPosition == transform.position)
			{
				SelectUnit(true);
			}
        }
	}

    if(Input.GetKeyDown(KeyCode.Mouse1)) // right click
    {
        playerPlane = new Plane(Vector3.up, transform.position);
        ray = Camera.main.ScreenPointToRay (Input.mousePosition);
       
        if (playerPlane.Raycast(ray, hitDist)) {
            targetPosition = ray.GetPoint(hitDist);
            var targetRotation = Quaternion.LookRotation(targetPosition - transform.position);
            transform.rotation = targetRotation;
			currentSpeed = walkSpeed;
        }
    }
	
	if(Vector3.Distance(transform.position, targetPosition) > .1)
	{
		transform.position += transform.forward * (currentSpeed * Time.deltaTime);
	}
	else
	{
		transform.position = targetPosition;
		currentSpeed = idleSpeed;
	}
}

function GetSpeed()
{
	return currentSpeed;
}

/**
 *	(bool) select - to select the unit or not
 */
function SelectUnit(select)
{	
	resetSelectedUnits();
	selected = select;
	
	// add it to the list of selected units
	if(selected)
	{
		selectedUnits.push(this);
	}
	// remove it from the list of selected units (if it exists)
	else
	{
		var index = selectedUnits.indexOf(this);
		if(index != -1)
		{
			selectedUnits.splice(index, 1);
		}
	}
	
	// todo: show selection projection
	Debug.Log(selectedUnits);
}

function resetSelectedUnits()
{
	selectedUnits = [];
}

@script RequireComponent(CharacterController)
@script AddComponentMenu ("Third Person Player/Third Person Controller")

This error doesn’t seem to make sense, but I suspect that Unity doesn’t know that playerPlane is a plane. Try to define its type in the declaration, like this:

var playerPlane: Plane; // explicitly declare the type
var ray: Ray; // declare this one too

It’s always advisable to define the variable type explicitly, like above, or implicity, by assigning a value of the correct type to it. If not defined, the compiler assumes a “generic” type, which is slower and may render you a lot of headaches.

Another headache maker: float comparisons. Comparing floats almost never produces an equality, unless they are whole numbers. Thus, when comparing clickedPosition it’s better to allow for some error margin, like this:

     var error: float = Vector3.Distance(clickedPosition, transform.position);
     if (error < 0.2)
     {
         SelectUnit(true);
     }

I fixed it, used Physics.Raycast instead.