Raycast switching object's color on and off.

What I'm looking for is a way to use this raycast script to switch back and forth. I.e. tap my object once for on and again for off. Any ideas?

var deadReplacement : GameObject; var deadReplacementPosition : Vector3; var deadReplacementRotation : Quaternion;

var hit : RaycastHit; var dieSound : AudioClip; var initialDelay = 1.0; var fadeSpeed = 1.0; var explosion : Transform; var damagesound : AudioClip; var myTransform : Transform;

function Awake() { myTransform = transform; // this objects transform }

function Update () { for (var evt : iPhoneTouch in iPhoneInput.touches) { if (evt.phase == iPhoneTouchPhase.Began) { var ray = camera.main.ScreenPointToRay(evt.position); if (Physics.Raycast(ray, hit, 10) ) { var hitObject : GameObject = hit.transform.gameObject; var hitTransform : Transform = hitObject.transform; if ( hitObject.tag == "Player" ) { if ( myTransform.position.x == hitTransform.position.x) { // matches x position if ( myTransform.position.z == hitTransform.position.z) { if ( myTransform.position.y == hitTransform.position.y) { // matches z position // so this is the object touched in the 2d space deadReplacementPosition = hit.transform.position; deadReplacementRotation = hit.transform.rotation;

  renderer.material.color.b = 0.0;
  renderer.material.color.g = 50.0;
  renderer.material.color.r = 0.0;


else {

` renderer.material.color.b = 0.0;
renderer.material.color.g = 0.0;
renderer.material.color.r = 50.0;



Is the code not working? Does it compile?

Some suggestions.

If it's simply not doing anything, break down the problem. First figure out if the tap code is working. One simple way is to call Debug.Log in each bit of code. First after the tap, then after the raycast, and lastly when deciding on the color. See where the code actually goes. Maybe the tap works and the raycast doesn't - maybe the player is more than 10 units from the screen? Maybe the ray hits something but it's the wrong object. Etc.

If I had to guess, I'd say it's the test for the Player tag that's messing you up. My guess is you have this script on an object that isn't tagged Player. If so, either get rid of that test or check that the object that this script is on is tagged Player.

Another possibility is that the object you want to be tapped doesn't have a collider on it, so the raycast will not detect it. Or, it's too far from the screen point and you'll have to increase the raycast distance.

A more elegant design for this, instead of every object having this script on it and doing a raycast, you could run this code only once, and send a "tapped" message to whichever object was hit. Some objects would ignore the message, others would react, if they wanted.

Also, I don't see why you need to test myTransform positon is equal to hitTransform position. First of all, comparing floating point numbers is inherently weak. The slightest numerical difference breaks the test. Secondly, you are already testing the hit object's tag == Player.

Hope one of these suggestions helps.