Search Unity

Selective Touch to Destroy gameObject

Discussion in 'iOS and tvOS' started by KeepTrying, Mar 9, 2011.

  1. KeepTrying

    KeepTrying

    Joined:
    Feb 23, 2011
    Posts:
    119
    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 ! :confused:

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

    Code (csharp):
    1. static var turnOn:boolean=true;
    2.  
    3. function Start(){
    4.  turnOn=true;
    5. }
    6.  
    7. function Update () {
    8.     for (var i = 0; i < Input.touchCount; i++){
    9.         var touch : Touch = Input.touches[i];
    10.         if (touch.phase == TouchPhase.Began) {
    11.             if(turnOn){
    12.             audio.Play();
    13.             Destroy(gameObject);
    14.             }
    15.         }          
    16.     }
    17. }
    SELECT LEVEL CODE Touch To Start Unlocked Levels

    Code (csharp):
    1. var lock:Texture;
    2. var loadlevel:int;
    3. private var zoom:boolean=false;
    4.  
    5. function Update () {
    6.     for (var i = 0; i < Input.touchCount; i++){
    7.         var touch : Touch = Input.touches[i];
    8.         if (touch.phase == TouchPhase.Began) {
    9.             zoom=true;
    10.             if(transform.localScale.x <= 0.95  renderer.material.mainTexture != lock){
    11.             transform.localScale.x += 3 * Time.deltaTime;
    12.             transform.localScale.y += 3 * Time.deltaTime;
    13.             }
    14.         }
    15.         if (touch.phase == TouchPhase.Stationary) {
    16.             zoom=false;
    17.             if(!zoom  renderer.material.mainTexture != lock){
    18.             if(transform.localScale.x >= 0.81){
    19.             transform.localScale.x -= 2 * Time.deltaTime;
    20.             transform.localScale.y -= 2 * Time.deltaTime;
    21.                 }
    22.             }
    23.         }
    24.         if (touch.phase == TouchPhase.Ended) {
    25.             if(renderer.material.mainTexture != lock){
    26.             Application.LoadLevel(loadlevel);
    27.             }
    28.         }              
    29.     }
    30. }
     
    Last edited: Mar 13, 2011
  2. jingato

    jingato

    Joined:
    Jun 23, 2010
    Posts:
    299
    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
     
  3. KeepTrying

    KeepTrying

    Joined:
    Feb 23, 2011
    Posts:
    119
    Added more details about the code !
     
    Last edited: Mar 9, 2011
  4. jingato

    jingato

    Joined:
    Jun 23, 2010
    Posts:
    299
    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
     
  5. KeepTrying

    KeepTrying

    Joined:
    Feb 23, 2011
    Posts:
    119
    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
     
    Last edited: Mar 10, 2011
  6. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    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:-
    Code (csharp):
    1. function OnMouseDown() {
    2.   Destroy(gameObject);
    3. }
     
  7. KeepTrying

    KeepTrying

    Joined:
    Feb 23, 2011
    Posts:
    119
    Thanks for your reply, but i need that working on iPhone !