Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Touch Script

Discussion in 'Scripting' started by Tyreor, Sep 2, 2015.

  1. Tyreor

    Tyreor

    Joined:
    Jul 24, 2015
    Posts:
    6
    Hello. When I touch one object, all object (clones) is destroyed. How can i fix it ?


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class DestroyObjectesEasy : MonoBehaviour
    6. {
    7.    
    8.     public static int myScore1 = 10;
    9.     public static int Highscores=0;
    10.     public static bool isDead=false;
    11.  
    12.  
    13.     public AudioClip shootSound;
    14.     public AudioSource source;  
    15.    
    16.     public float volLowRange = .5f;
    17.     public float volHighRange = 1.0f;              
    18.  
    19.  
    20.     public void awake()
    21.     {
    22.         source = GetComponent<AudioSource>();
    23.     }      
    24.    
    25.  
    26. /*    void OnMouseDown()
    27.     {
    28.         isDead = true;
    29.         Destroy (this.gameObject);
    30.         StartSinking ();
    31.     }*/
    32.  
    33.     void Update() {
    34.  
    35.          
    36.         if (Input.touchCount > 0)
    37.         {
    38.             int i = 0;
    39.             for (i = 0; i < Input.touchCount; i++)
    40.             {
    41.                 Destroy(this.gameObject);
    42.             }
    43.                 //Do something with the touches
    44.  
    45.                 isDead = true;
    46.                 //Destroy (this.gameObject);
    47.                 Scoring ();
    48.  
    49.                 //if (Input.GetButtonDown ("Fire1")) {
    50.                 float vol = Random.Range (volLowRange, volHighRange);
    51.  
    52.                 if (isDead == true)
    53.                     source.PlayOneShot (shootSound);              
    54.                 isDead = false;      
    55.             }
    56.         }      
    57.     public void Scoring () {
    58.         ScoreManagerEasy.score1 += myScore1;
    59.    
    60.     }
    61. }
    62.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I assume that code is on each of the clones?

    it basically reads "if there is a touch (anywhere) destroy this gameobject"

    you want it to read

    "if there is a touch over this game object, destroy this gameobject" (or something similar depending on how you want your game to work :p)
     
  3. mahin_1991

    mahin_1991

    Joined:
    Mar 15, 2014
    Posts:
    1
    you are basically destroying game object on touch itself. So this script attached to every gameobject destroys.
     
  4. Tyreor

    Tyreor

    Joined:
    Jul 24, 2015
    Posts:
    6
    Can I throw this script to the camera and the camera somehow destroy him?
     
  5. Tyreor

    Tyreor

    Joined:
    Jul 24, 2015
    Posts:
    6
    What I must to do, that after double-tap in two objects Both were destroyed?
    Code (csharp):
    1.  
    2. if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
    3.  
    4.        Ray ray = Camera.main.ScreenPointToRay( Input.GetTouch(0).position);
    5.    
    6.        RaycastHit hit;
    7.  
    8.  
    9.        int i = 0;
    10.        for (i = 0; i < Input.touchCount; i++){
    11.  
    12.          if ( Physics.Raycast(ray, out hit) && GameObject.FindGameObjectWithTag("RollerBallEasy"))
    13.         {
    14.          Destroy(hit.transform.gameObject);
    15.            isDeadEasy = true;
    16.        
    17.          }
    18.        }
    Problem is resolved :
    Code (csharp):
    1.  
    2.   void Update() {
    3.      RaycastHit hit;
    4.      for (var i = 0; i < Input.touchCount; ++i) {
    5.        Ray ray = Camera.main.ScreenPointToRay( Input.GetTouch(i).position);
    6.        if (Input.GetTouch(i).phase == TouchPhase.Began)      
    7.         {
    8.        
    9. }
    10.         }
    11.        }
     
    Last edited: Sep 5, 2015