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. Dismiss Notice

Question Enemy gets destroyed instead of changing colour

Discussion in 'Scripting' started by JMoore5, Jun 25, 2023.

  1. JMoore5

    JMoore5

    Joined:
    Apr 28, 2020
    Posts:
    3
    When a player's bullet collides with a specific enemy I want to have it change color and once it gets hit again it gets destroyed. With this code, it just gets destroyed on the first collision and I'm not sure why. aa.JPG
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Code (CSharp):
    1. private void OnTriggerEnter(Collider other)
    2. {
    3.     if (other.gameObject.name=="Bullet(Clone)")
    4.     {
    5.         Destroy(other.gameObject);
    6.         if (ren.material.color==Color.red)
    7.             Destroy(gameObject);
    8.         else
    9.             ren.material.color=Color.red;
    10.     }
    11.     else if (other.gameObject.name=="Player")
    12.     {
    13.         Destroy(gameObject);
    14.     }
    15. }
     
    JMoore5 likes this.
  3. JMoore5

    JMoore5

    Joined:
    Apr 28, 2020
    Posts:
    3
    This worked perfectly, Thank you!