I have little devils that I want to kill when someone touches them on an iphone screen. Heres the code. Each object in the game has this code attached to it. Can anyone explain why it kills them all if I touch one of them and how to only kill the one that was touched??
Thanks in advance.
In function Update:
for (var i = 0; i < Input.touchCount; ++i) {
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
var MyCast : RaycastHit;
var ObjectHit : GameObject;
var ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
if(Physics.Raycast(ray,MyCast,100))
{
ObjectHit = MyCast.collider.gameObject;
ObjectHit.SendMessage("OnMouseDown");
//transform.gameObject.SendMessage("OnMouseDown");
}
}
}
then:
function OnMouseDown ()
{
var Smoke = gameObject.transform.FindChild("Smoke");
var Body = gameObject.transform.FindChild("character");
HitPoints = HitPoints - 1;
if(HitPoints <= 0)
{
Body = Body.transform.FindChild("ROOT");
//Debug.Log(Body2);
if(DevilCanMove == true)
{
PlayerPrefs.SetInt("CurrScore",(PlayerPrefs.GetInt("CurrScore") + 10)); //increase the score
}
DevilCanMove = false; //stop running
transform.rotation = Quaternion.LookRotation(Vector3(0,1,0)); //look up when you die little devil
animation.Play("gets_killed"); //lay down and die!!
Smoke.active = true;
yield WaitForSeconds (1);
Body.active = false;
yield WaitForSeconds (1);
Smoke.active = false;
//yield WaitForSeconds (2); //wait to make the player happy
Destroy(gameObject); //poof - devil is gone
gameGUI.ReduceDevils(); //reduce devil count for end of level
}
else
{
DevilCanMove = false;
animation.Play("getting_hit");
yield WaitForSeconds(.5);
if(DevilCanMove == false)
{
PlayerPrefs.SetInt("CurrScore",(PlayerPrefs.GetInt("CurrScore") + 10)); //increase the score
}
DevilCanMove = true;
}
}