Search Unity

Bullet destroy 2 game object intent one unity 2D

Discussion in '2D' started by theriyazsaifi_unity, Apr 17, 2019.

  1. theriyazsaifi_unity

    theriyazsaifi_unity

    Joined:
    Apr 1, 2019
    Posts:
    4
    When my bullet Collides with a Ball if there is 2 Balls at near or at the same position, all object get destroyed at same time. I set in my script when bullet collide with other then player it will be destroy.

    I was trying to check every ball type with else if


    Code (CSharp):
    1. if(other.gameObject.tag == "yerrow")
    2.     {
    3.         if (ballType >= 0 && ballType < 4)
    4.         {
    5.  
    6.             clone1 = (GameObject)Instantiate(_ball, gameObject.transform.position, Quaternion.identity);
    7.             clone1.gameObject.GetComponent<Balloon_Behave>().ballMove = -4;
    8.             clone1.gameObject.GetComponent<Balloon_Behave>().transform.position = new Vector2(transform.position.x - Random.Range(0, 0.5f), transform.position.y - Random.Range(0,1));
    9.  
    10.             clone2 = Instantiate(_ball, gameObject.transform.position, Quaternion.identity);
    11.  
    12.             Destroy(this.gameObject);
    13.         }
    14.         else if (ballType == 4)
    15.         {
    16.             Destroy(this.gameObject);
    17.         }
    18.         Debug.Log("Yerrow COLl");
    19.     }
    20. Yerrow Script Destroy himself
    21.  
    22.   private void OnTriggerEnter2D(Collider2D other)
    23.       {
    24.          if (other.gameObject.tag != "Player")
    25.           {
    26.              Destroy(this.gameObject);
    27.              player.CanFire = true;
    28.           }
    29.       }
    30. EDIT NOTE : Every ball collides with bulled is destroy intent one. I want to destroy one object by one bullet
     
  2. N_Murray

    N_Murray

    Joined:
    Apr 1, 2015
    Posts:
    98
    I use a boolean to avoid this, for instance if your bullet has a bool called hit and it is initially set to false when you call your onTriggerEnter check if hit == false then destroy. Then obviously set it to true so the next object to hit it will not be destroyed
     
  3. Rockaso

    Rockaso

    Joined:
    Oct 31, 2016
    Posts:
    66
    Did you check the Ball's collider size? Does it match the sprite?
     
unityunity