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

Enemy take damage

Discussion in 'Scripting' started by tomasvyo, Sep 17, 2021.

  1. tomasvyo

    tomasvyo

    Joined:
    Mar 5, 2020
    Posts:
    1
    I'm trying to enemy take damage which is set on Shooting script.

    Enemy Script:
    Code (CSharp):
    1. public class SimpleAI : MonoBehaviour
    2. {
    3.  
    4.     [SerializeField] Transform target;
    5.     NavMeshAgent agent;
    6.     public GameObject player;
    7.     private Shooting shootingScript;
    8.  
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         agent = GetComponent<NavMeshAgent>();
    14.         agent.updateRotation = false;
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         agent.SetDestination(target.position);
    21.         if(Health == 0)
    22.         {
    23.             Destroy(gameObject);
    24.         }
    25.     }
    26.  
    27.     public void PlayerDamage()
    28.     {
    29.         gameObject player = gameObject.Find("Player");
    30.         shootingScript = player.GetComponent<Shooting>();
    31.     }
    32.  
    33.     //Health
    34.     public int Health = 5;
    35.  
    36.     private void OnTriggerEnter2D(Collider2D other)
    37.     {
    38.         Health -= player.damage;
    39.     }
    40.  
    41. }
    42.  
    Shooting script:
    Code (CSharp):
    1. public class Shooting : MonoBehaviour
    2. {
    3.     public Transform firepoint;
    4.     public GameObject bulletPrefab;
    5.     public float fireRate = 0.5F;
    6.     private float nextFire = 1.5F;
    7.     public int maxAmmo = 10;
    8.     private bool isReloading = false;
    9.     private int currentAmmo;
    10.     public float bulletForce = 20f;
    11.     public float reloadTime = 1f;
    12.     public Text ammoUI;
    13.     public GameObject reloading;
    14.     public GameObject ammoUi;
    15.     public int damage = 1;
    16.  
    17.     // Update is called once per frame
    18.     void Start()
    19.     {
    20.         ammoUi.SetActive(true);
    21.     }
    22.     void Update()
    23.     {
    24.         ammoUI.text = currentAmmo.ToString();
    25.  
    26.         if (isReloading)
    27.         {
    28.             return;
    29.         }
    30.         if(currentAmmo <= 0)
    31.         {
    32.             StartCoroutine(Reload());
    33.             return;
    34.         }
    35.         if(Input.GetButtonDown("Fire1")&& Time.time > nextFire)
    36.         {
    37.             nextFire = Time.time + fireRate;
    38.             Shoot();
    39.  
    40.         }
    41.     }
    42.     IEnumerator Reload()
    43.     {
    44.         isReloading = true;
    45.         reloading.SetActive(true);
    46.         Debug.Log("Reloading");
    47.         yield return new WaitForSeconds(reloadTime);
    48.         currentAmmo = maxAmmo;
    49.         reloading.SetActive(false);
    50.         isReloading = false;
    51.     }
    52.    
    53.  
    54.     void Shoot()
    55.     {
    56.        
    57.  
    58.         currentAmmo--;
    59.    
    60.  
    61.         GameObject bullet = Instantiate(bulletPrefab, firepoint.position, firepoint.rotation);
    62.         Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
    63.         rb.AddForce(firepoint.up * bulletForce, ForceMode2D.Impulse);
    64.     }
    65. }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You need to help us, help you mate. What's not working? Is OnTriggerEnter2D being called (add a Debug.Log to it)? If not, does the bullet have a trigger collider?
     
  3. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Lines 29 and 30 of enemy script would be better placed in awake or start function, no? Also remove "gameObject" before finding reference to player. You are creating a new variable for player which I assume will be leaving your original reference null. The variable type is already assigned so no need to do again. Only

    Code (csharp):
    1.  player = gameObject.Find("player");
    Something like that in start.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
  5. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    It's fine to use it in start or awake, with smaller projects. Yes there are better ways but also not a problem if used sparingly and correctly.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    And yet, look how many new users post in here because
    GameObject.Find("Player");
    fails to find their player, which happens to be named
    Player (Clone)
    .

    The issue is that GameObject.Find() has 27 different ways to fail, most of them non-obvious, and a new user cannot reason about any of that. They wouldn't even know where to begin!

    I stand by the above. Sure, if you're advanced and can reason about how it can fail, use it. It's handy. But for anyone else, as the official docs recommend, do not use it.