Search Unity

Destroy object A when i press on object B

Discussion in 'Scripting' started by dontr, Jun 29, 2017.

  1. dontr

    dontr

    Joined:
    Jun 29, 2017
    Posts:
    8
    Im new to scripting guys so i need some help.

    I want to detroy every instantiated A object that rolls over object B only when i press on object B. ( so yeah, B its a gameobject that is also used as a button with raycast). Here is what i tried to do on my own.

    Code (CSharp):
    1.  void Update () {
    2.         if (Input.GetMouseButtonDown(0))
    3.         {
    4.             Debug.Log("Mouse is down");
    5.             // Trying to detect when i press on object B that has the tag "cubes"
    6.  
    7.             RaycastHit hitInfo = new RaycastHit();
    8.             bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
    9.             if (hit)
    10.             {
    11.                 Debug.Log("Hit " + hitInfo.transform.gameObject.name);
    12.                 if (hitInfo.transform.gameObject.tag == "cubes")
    13.                 { //here is deleting all the instantiated object but i want to be deleted only the one colliding with B
    14.                     Destroy(gameObject);
    15.                 }
    16.              
    17.             }
    18.             else
    19.             {
    20.                 Debug.Log("No hit");
    21.             }
    22. }
    Please be kind and help me. :)
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,539
    Have a trigger collider on object B, kinematic rigidbody

    When objects A enter it (OnTriggerEnter), keep them in a HashSet or List.
    When objects A leave it (OnTriggerExit), remove them.
    When clicked (OnMouseDown), delete the contents of the collection.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    So you've got a few solid ideas here... let me make a few notes:

    Line 2: this will only be true for one frame, the frame at which the mouse button goes down. If you want it for as long as the mouse button IS down, then use .GetMouseButton(0) instead.

    Line 14: lowercase gameObject is shorthand for "my gameobject here, the one this script is on." I don't think you want to destroy that.

    Instead you can use the hitInfo.transform.gameObject and destroy that instead. That will destroy the object you click on.

    However, I think you want something else that ... well, I see someone above suggested triggers, so look down that route! :)
     
  4. dontr

    dontr

    Joined:
    Jun 29, 2017
    Posts:
    8
    First of all i want to thank you for your time and for your answers. :)
    Now:

    On line 2, i only want when i click on it, not when i hold it. However, thats a good info and be sure i will use it in the future. Also, Line 14 you are right. didnt know its a shorthand. Very useful. Hoever i change that remove to the triggers way.

    thank you for your ideas. I tried this way but as im noob, i couldnt do it :)

    here is my code:
    Code (CSharp):
    1. List<GameObject> currentCollisions = new List<GameObject>();
    2.  
    3.     // Use this for initialization
    4.     void Start () {
    5.        
    6.     }
    7.     void OnCollisionEnter(Collision col)
    8.     {
    9.  
    10.         // Add the GameObject collided with to the list.
    11.         currentCollisions.Add(col.gameObject);
    12.         Debug.Log(gameObject.name);
    13.     }
    14.  
    15.     void OnCollisionExit(Collision col)
    16.     {
    17.  
    18.         // Remove the GameObject from the list.
    19.         currentCollisions.Remove(col.gameObject);
    20.      
    21.     }
    22.  
    23.    
    24.  
    25.  
    26.     // Update is called once per frame
    27.     void Update () {
    28.         if (Input.GetMouseButtonDown(0))
    29.         {
    30.             Debug.Log("Mouse is down");
    31.  
    32.             RaycastHit hitInfo = new RaycastHit();
    33.             bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
    34.             if (hit)
    35.             {// detecting the click on object B
    36.                 Debug.Log("Hit " + hitInfo.transform.gameObject.name);
    37.                 if (hitInfo.transform.gameObject.tag == "cubes")
    38.                    
    39.                 {
    40.                    
    41.                     // remove and destroy all objects that are currently on object B (stored in the list)
    42.  
    43.                     foreach (GameObject go in currentCollisions)
    44.                     {
    45.                        
    46.                         GameObject.Destroy(go);
    47.                         currentCollisions.Remove(go);
    48.                     }
    49.                    
    50.                 }
    51.                 else
    52.                 {
    53.                     Debug.Log("nopz");
    54.                 }
    55.             }
    56.             else
    57.             {
    58.                 Debug.Log("No hit");
    59.             }
    60.         }
    61.  
    62.  
    63.     }
    Here is the error i get: "Collection was modified. Enumeration operation may not execute."

    Also the script from above is added to object A. Not sure if this should be added to B.
    Trigger collider on object B, kinematic rigidbody.



    Thank you again for your time.
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,539
  6. dontr

    dontr

    Joined:
    Jun 29, 2017
    Posts:
    8
    Wow. Ok. I removed the Update/Raycast for :
    Code (CSharp):
    1.  void OnMouseDown()
    2.     {if(gameObject.tag == "cubes")
    3.         Debug.Log(gameObject.name);
    4.     }
    Much better.

    About theOnTriggerEnter, and OnTriggerExit i see that they are different. But how do i add them to a list if not like this ?
    Code (CSharp):
    1. List<GameObject> currentCollisions = new List<GameObject>();
    2.  
    3. void OnTriggerEnter(Collider col)
    4.     {
    5.  
    6.         // Add the GameObject collided with to the list.
    7.         currentCollisions.Add(col.gameObject);
    8.         //Debug.Log(gameObject.name);
    9.     }
    10.  
    11.     void OnTriggerExit(Collider col)
    12.     {
    13.  
    14.         // Remove the GameObject from the list.
    15.         currentCollisions.Remove(col.gameObject);
    16.      
    17.     }
     
  7. dontr

    dontr

    Joined:
    Jun 29, 2017
    Posts:
    8
    Nevermind. It works now. Triggers from above work jusst fine! Thankyou for everything!