Raycast issue ...

I am attempting to light up the possible tiles on the grid whilst dragging the finger along the screen…
I have it working well with the mouse thats where the onmouseover and onmousexit comes in handy by lighting up the tile which the mouse is over by enabling the renderer and then changing the color then when the mouse exits it turns it off…I am trying to achieve the same effect on an IOS device…the lighting up works correctly though the squares do not disable themselves when the touch position has left or has moved to another tile…

How would I get the tile to turn off when the finger touch has moved?? Touch.Phase Ended doesn’t work as it lights up the tiles when dragging over them and turns them off all together instead of when the finger is no longer on that tile…sorry about the lengthy description!

#pragma strict

//WhateverGrid.js
    var gridData : DefenseGrid;
	
    var myCast : RaycastHit;

	var rayLength : float = 300;
	
	static var pointerPosition : Vector2;
			
    function Start(){
        gridData = GameObject.Find("Grid").GetComponent("DefenseGrid");
        
    }

    function OnMouseOver(){
    
       	       renderer.enabled = true;

        renderer.material.color = Color.red;
    }
    
    function OnMouseExit(){
       
       renderer.enabled = false;
    }
    
    
function Update () {



var touch : Touch;

 if (Input.touchCount > 0)

        {

        touch = Input.touches[0];

        if(touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved){             

			if (Physics.Raycast(Camera.main.ScreenPointToRay(touch.position), myCast, rayLength)) {
			
				 var objectHit : GameObject;
   				 objectHit = myCast.collider.gameObject;
   				 if(objectHit.tag == "platform"){
   				 objectHit.renderer.enabled = true;
				 objectHit.renderer.material.color = Color.red;
				 
		 		}
		 	}

        }
        
}
}

It sounds like you have two options:

  1. You can create your own version of the MouseEnter and MouseExit functions for a touch object so that each object keeps track of whether it is currently being “touched” using the raycast. This can become very inefficient depending on the number of objects you have that will be doing their own raycast during any given Update().
  2. You can do a single raycast and keep track of what object is currently being touched. Each time you need to switch objects to a new one, do whatever “MouseExit” type functionality you need to do on the prior object before setting the new object as the “current” and running your “MouseOver” functionality on it.