Raycast off

Hey Guys
I have an inventory script going and a loot system set up to loot on pressing E once close enough and looking at the object with a raycast.
When I look at the object I have it set to fade the color in and out to make a lootable object flash. My problem is that when I take my aim off it, I need it to go back to not being transparent. Where as if I look at another lootable object before its all the way back to full color this does not work and the previous object stays transparent.

This is the looting script that controls the raycast for looting:

var lootDistance : int = 3;
var theSystem : Transform;

private var lastObjHit : RaycastHit;
private var drawGUI = false;
private var hit : RaycastHit;
private var lootable : boolean = false;

function Update () 
{
    if(Physics.Raycast (theSystem.transform.position, theSystem.transform.TransformDirection(Vector3.forward), hit, lootDistance))
    {
        if(hit.collider.gameObject.CompareTag("Loot"))
        {
        	drawGUI = true;
        	hit.transform.SendMessage("Lootable", true, SendMessageOptions.DontRequireReceiver);
        	lastObjHit = hit;
        	lootable = true;
        }
        else if(hit.collider.gameObject.CompareTag("Untagged"))
        {
        		drawGUI = false;
        		lastObjHit.transform.SendMessage("Lootable", false);
        }
        if(Input.GetKeyDown(KeyCode.E) && drawGUI == true && hit.collider.gameObject.CompareTag("Loot"))
        {
        	hit.transform.SendMessage("Loot", SendMessageOptions.DontRequireReceiver);
        	drawGUI = false;
        	lootable = false;
        }
	}
	else
    {
       	drawGUI = false;
       	
       	if(lootable == true)
       	{
       		lastObjHit.transform.SendMessage("Lootable", false);
       		lootable = false;
       	}
    }
}

function OnGUI ()
{
	if (drawGUI == true)
	{
		GUI.Box (Rect (Screen.width*0.5-55, 200, 110, 22), "Press E to Loot");
	}
}

This is the lootable object code:

var inventory : GameObject; // represents a reference to inventory

private var decr : boolean = false;

function Start()
{
	decr = false;
}

function Lootable(toggle : boolean )
{
	if(toggle)
	{
		ToggleAlpha(0.3,1);
	}
	else
	{
		renderer.material.color.a = 1;
	}
}

function Loot()
{
		renderer.material.color.a = 1;
		gameObject.Find("Inventory").GetComponent(PositionController).SaveObjectInInventory(transform.gameObject);
}

function ToggleAlpha(minV : float, maxV : float)
{
	if(renderer.material.color.a > minV && decr)
	{
		renderer.material.color.a -= 1*Time.deltaTime;
	}
	else
	{
		renderer.material.color.a += Time.deltaTime;
	}
	
	if(renderer.material.color.a > maxV)
	{
		decr = true;
	}
	else if(renderer.material.color.a < minV)
	{
		decr = false;
	}
}

Your problem is that I don’t believe you understand sendMessage.

else
{
drawGUI = false;

    if(lootable == true)
    {
        lastObjHit.transform.SendMessage("Lootable", false);
        lootable = false;
    }
}

right there, you are say if the raycast doesn’t work, then go into lastObjHit, however raycasts, will always work, unless stated in the previous if statment.

Using your logic, you would want to use something like this

if(Physics.Raycast (theSystem.transform.position, theSystem.transform.TransformDirection(Vector3.forward), hit, lootDistance))
    {
        if(hit.collider.gameObject.CompareTag("Loot"))
        {
            drawGUI = true;
            hit.transform.SendMessage("Lootable", true, SendMessageOptions.DontRequireReceiver);
            lastObjHit = hit;
            lootable = true;
        }
        else 
        {
               drawGUI = false;
               lastObjHit.transform.SendMessage("Lootable", false);
}

Here you are saying that if your raycast works, and what it is hiting is the “Loot”, then proceed with your loot logic. However if what you arn’t hitting is “Loot” Then make the loot’s transparency back to full.

Let me know if that works.

NOTE: Instead of using your logic like this, take a look at Vector3.Distance, and line of sight logic. I believe that you can get better results that way.