Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

how to make enemy killed by bullet unity2D

//Put this code on your enemy
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;

  public class EnemyScript : MonoBehaviour
  {
    float health = 10;
    // use OnCollisionEnter2D if you don't have trigger on your bullet
    void OnTriggerEnter2D(Collider2D other)
        {
            if(other.gameObject.CompareTag("Bullet"))
            {
                // You just put script where you have saved your bullet's damage 
                float Damage = other.GetComponent<BulletScript>.Damage;
                health -= Damage;
                if(health <= 0)
                {
                    Destroy(gameObject);
                }
            }
      }
  }
//Put this on your bullet and give your bullet new tag "Bullet"
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;

  public class BulletScript : MonoBehaviour
  {
      public Damage = 5;
  }
// after that, enemy will get destroyed after getting touched at least twice
// by your bullet;
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# fold sum array 
Csharp :: infinit range loop c# 
Csharp :: constant interpolated string 
Csharp :: c# ilogger for inherited class 
Csharp :: MissingMethodException: PlayerManager.OnPlayerLeft Due to: Attempted to access a missing member. 
Csharp :: c# accept any enum 
Csharp :: asp.net session empty cehck 
Csharp :: how to assign 2d physics material through script 
Csharp :: how to list all registered users asp net 
Csharp :: "using" c# 
Csharp :: how do you search for how many times a character appears in user input on c sharp 
Csharp :: pyqt send message to another instance 
Csharp :: edit database from datagridview with right click on data c# 
Csharp :: negative indexing in c# 
Csharp :: c# skip debug attribute 
Csharp :: how to show messagebox 
Csharp :: convert string csv line to list c# 
Csharp :: C# calling method name 
Csharp :: what is the difference between rotation rotation axis and equator 
Csharp :: c# is file closed 
Csharp :: SerializedObjectNotCreatableException: Object at index 0 is null 
Csharp :: backcolor app winform C3 
Csharp :: c# if loop 
Csharp :: scroll two divs simultaneously site:stackoverflow.com 
Csharp :: the range data annotation attribute (Double) 
Csharp :: c# encrypt folder SHA512 
Csharp :: c# ef dynamic ApplyConfiguration 
Csharp :: asp net route attribute vs httpget 
Csharp :: bash clean-up code 
Csharp :: asp.net mvc class="" inline select 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =