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

Destroying a 2D object with a click

Discussion in 'Scripting' started by SoulbladeSBA, Mar 6, 2017.

  1. SoulbladeSBA

    SoulbladeSBA

    Joined:
    Feb 20, 2016
    Posts:
    43
    Hi, I am trying to destroy a 2D object by just clicking on the image but it does not work. IS there a setting that you need or component that needs to be added when creating a "D U>I element to enable it to be inter-actable via a mouse click as my debug is not even returning a message.
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    What have you done so far for your destroy-on-click behaviour?

    Common mistake is not including the EventSystem gameobject in the scene.
    Another possibility is having RaycastTarget set to false on your button UI object.
     
    Last edited: Mar 6, 2017
  3. booiljoung

    booiljoung

    Joined:
    May 12, 2013
    Posts:
    57
    Hi

    Could you show me your "Hierarchy", "Inspector", "Script code"?
     
  4. SoulbladeSBA

    SoulbladeSBA

    Joined:
    Feb 20, 2016
    Posts:
    43
    sorry for the misunderstanding, its not a button, its a U.I image
    inspector.png hierachy.png

    and here's the code:
    Code (CSharp):
    1.    void OnMouseDown()
    2.     {
    3.         Debug.Log("I have been clicked YAYAYAYYAYA ");
    4.    
    5.    
    6.     }
     
  5. SoulbladeSBA

    SoulbladeSBA

    Joined:
    Feb 20, 2016
    Posts:
    43
    also does it matter if another object in the scene has the Onmousedown function attached to it on another script?
     
  6. shashank1981

    shashank1981

    Joined:
    Feb 20, 2017
    Posts:
    24
    Last edited: Mar 6, 2017
  7. SoulbladeSBA

    SoulbladeSBA

    Joined:
    Feb 20, 2016
    Posts:
    43
    is there no possible way to give the raw image a level of interactivity?
     
  8. shashank1981

    shashank1981

    Joined:
    Feb 20, 2017
    Posts:
    24
    Yes, it can be given n thats what I meant by option 1