Search Unity

Destroy object newbie help

Discussion in '2D' started by skeleton-king, Jan 19, 2015.

  1. skeleton-king

    skeleton-king

    Joined:
    Nov 10, 2014
    Posts:
    63
    I am trying to destroy 2d object when clicked on it

    Heres my code
    if (Input.GetButton("Fire1"))
    Destroy(this.gameObject);

    But if i have 2 instance of object, both gets destroyed when i click on one object.
    How do i just destroy one object instance that i click
     
  2. adam_mehman

    adam_mehman

    Joined:
    Dec 11, 2014
    Posts:
    104
    What object is your script assigned to?
     
  3. skeleton-king

    skeleton-king

    Joined:
    Nov 10, 2014
    Posts:
    63
    script is attached to same object that i wana destroy when i click on it
     
  4. adam_mehman

    adam_mehman

    Joined:
    Dec 11, 2014
    Posts:
    104
    Ok and the other object is having that same script right? If yes that is the problem.

    You have to "catch" object you clicked on.
    Lets say, every time you click on object you have to check which one is clicked and then destroy that one.

    Try to add different "Tag" to each object, and then when you click on one check which "Tag" is it, and then destroy GameObject with that "Tag".

    Did you understand what i said?
     
  5. skeleton-king

    skeleton-king

    Joined:
    Nov 10, 2014
    Posts:
    63
    No
    I have a 2d object called apple which has this script.
    I put this object on scene and then i duplicate it to make 2 apples
    But now when i click on one apple both apple gets delete.
    How to make only delete the apple i click on it
     
  6. adam_mehman

    adam_mehman

    Joined:
    Dec 11, 2014
    Posts:
    104
    First: Code you showed remove from that script and put it in separate script and then that script attach for example, to Background game object. Tag of first apple should be "FirstApple" and Tag of the second one should be "SecondApple".

    Script on Background should have something like this

    Code (CSharp):
    1. void Update()
    2. {
    3.      if (Input.GetMouseButtonDown(0))
    4.      {        
    5.          RaycastHit hitInfo = new RaycastHit();
    6.          bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
    7.          if (hit)
    8.          {
    9.              if (hitInfo.transform.gameObject.tag == "FirstApple")
    10.              {
    11.                  //FindGameObjectsWithTag FirstApple
    12.                  //destroy that object
    13.              } else {
    14.                  //FindGameObjectsWithTag SecondApple
    15.                  //destroy that object
    16.              }
    17.          }
    18.      }
    19. }
     
  7. skeleton-king

    skeleton-king

    Joined:
    Nov 10, 2014
    Posts:
    63
    isnt there a better way to do this?
    If i wana have 30 apples does that mean i need to create 30 tags??
     
  8. adam_mehman

    adam_mehman

    Joined:
    Dec 11, 2014
    Posts:
    104
    Yes, try without tags:

    Code (CSharp):
    1. void Update()
    2. {
    3.      if (Input.GetMouseButtonDown(0))
    4.      {      
    5.          RaycastHit hitInfo = new RaycastHit();
    6.          bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
    7.          if (hit)
    8.          {
    9.              Destroy(hitInfo.transform.gameObject);
    10.          }
    11.      }
    12. }
     
  9. KaaPex

    KaaPex

    Joined:
    Jan 17, 2015
    Posts:
    16
    Hi!
    Create prefab of your apple, add two of them to the scene and after click use code
    Code (CSharp):
    1.  
    2. function OnMouseEnter () {
    3.  
    4. if (Input.GetKeyDown ("Mouse 0")) {
    5.  
    6. Destroy (gameObject); }
    7.  
    8. }`
    9.  
    or
    Code (CSharp):
    1. function OnMouseDown () {
    2.     Destroy(gameObject);
    3. }
    Add this script to your apple prefab.
     
    Last edited: Jan 19, 2015
  10. skeleton-king

    skeleton-king

    Joined:
    Nov 10, 2014
    Posts:
    63
    I saved it using prefabs but it still deletes both apples
     
  11. KaaPex

    KaaPex

    Joined:
    Jan 17, 2015
    Posts:
    16
    Here it is))
     

    Attached Files:

  12. skeleton-king

    skeleton-king

    Joined:
    Nov 10, 2014
    Posts:
    63
    prefab doesnt seem to have anything and code doesnt seem to delete apple at all:confused:
     
  13. KaaPex

    KaaPex

    Joined:
    Jan 17, 2015
    Posts:
    16
    Add script in archive on to prefab if you hadn't done this before.
     
  14. skeleton-king

    skeleton-king

    Joined:
    Nov 10, 2014
    Posts:
    63
    I did add script in inspector, it doesnt delete object
     

    Attached Files:

  15. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
  16. skeleton-king

    skeleton-king

    Joined:
    Nov 10, 2014
    Posts:
    63
    Thanks kaapex and dterbeest it works now