Selective Touch to Destroy gameObject

Hi folks - the objective of this script is to remove one object at time but when i touch in any where or any object the script remove all interactive objects in my game WHY ? The same happens in select level screen he load the first unlocked level when i touch in the screen ! :face_with_spiral_eyes:

DESTROY OBJECT CODE TurnOn and Off the capability of touch to destroy objects in scene !

static var turnOn:boolean=true;

function Start(){
 turnOn=true;
}

function Update () {
	for (var i = 0; i < Input.touchCount; i++){
		var touch : Touch = Input.touches[i];
		if (touch.phase == TouchPhase.Began) {
			if(turnOn){
			audio.Play();
			Destroy(gameObject);
			}
		}			
	}
}

SELECT LEVEL CODE Touch To Start Unlocked Levels

var lock:Texture; 
var loadlevel:int; 
private var zoom:boolean=false; 

function Update () {
	for (var i = 0; i < Input.touchCount; i++){
		var touch : Touch = Input.touches[i];
		if (touch.phase == TouchPhase.Began) {
			zoom=true;
			if(transform.localScale.x <= 0.95  renderer.material.mainTexture != lock){
			transform.localScale.x += 3 * Time.deltaTime;
			transform.localScale.y += 3 * Time.deltaTime;
			}
		}
		if (touch.phase == TouchPhase.Stationary) {
			zoom=false;
			if(!zoom  renderer.material.mainTexture != lock){
			if(transform.localScale.x >= 0.81){
			transform.localScale.x -= 2 * Time.deltaTime;
			transform.localScale.y -= 2 * Time.deltaTime;
				}
			}
		}
		if (touch.phase == TouchPhase.Ended) {
			if(renderer.material.mainTexture != lock){
			Application.LoadLevel(loadlevel);
			}
		}				
	}
}

I don’t rallt see what your script is doing. When does turnOn ever get changed. What is turnOn? And I don’t see you ever checking what objects you touched.

Also, please put your code in code tags next time. It makes it much easier to read

Added more details about the code !

Ok, I’m not sure what your doing here, but here’s some things I noticed.

  1. What is turnOn? You set it to true and it never gets set to false so why have it?
  2. If the first script, if you add it to a gameObject than that object will get destroyed whenever you touch the screen, no matter where you touch because you’re not checking the location of the touch and seeing what you’ve actually touched.
  3. I see you calling Application.LoadLevel(loadlevel), but I don’t se loadLevel ever being set.

for the first script, you don’t want to have something like that attached to every object you want to be able to delete. You want to have a single object check for the touches, get the location of the touch, and cast a ray to see what it has touched, then do what you want with the results.

Same goes for the second script, If you had it on multiple gameObjects it is going to call Application.LoadLevel(loadlevel); for each one every time you lift your finger

My game is like that http://tumbledrop.com/ so i need to remove some shapes to safely land the hero in the ground ! I need to set up one touch event for every shape in my scene to distinguish one to another ???

Simple xplain of my game:

1 - We got a platform with bad guys and good guys

2 - Under this platform we got a ground with a script to check if good guy or bad guy are falling

3 - Bad guys on ground you win - good guys you loose

4 - The Application.LoadLevel(loadlevel); is activated when you loose or win because the buttons Restart and Next Level appear on screen

You should check out the OnMouseDown function. If you have this function in your script then it will be called whenever the mouse is clicked over your object (it needs to have a collider to make this work). For example, to destroy the object, you would use:-

function OnMouseDown() {
  Destroy(gameObject);
}

Thanks for your reply, but i need that working on iPhone !