Search Unity

My RPG Rocket Wont delete itself due to get component script

Discussion in 'Scripting' started by Cptenglish, Sep 25, 2021.

  1. Cptenglish

    Cptenglish

    Joined:
    Apr 24, 2015
    Posts:
    8
    So I think this is my first posting here in a while, but I have an issue with my code for my rpg rocket.
    So the issues stems from the fact that when I shoot and it touches anything it gets destroyed. However when i implement the code to deal damage to my enemies suddenly the rocket doesnt destroy itself? But it works when I delete that line of code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class rpgRocketdestruction : MonoBehaviour
    6. {
    7.     public LayerMask enemylayers;
    8.     public float explosionradius;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.        
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.        
    19.     }
    20.     void OnDrawGizmosSelected()
    21.     {
    22.         Gizmos.DrawWireSphere(gameObject.transform.position, explosionradius); //for me to see the radius of the explosion
    23.     }
    24.     void OnCollisionEnter(Collision collision)
    25.     {
    26.         Collider[] enemiesinrange;
    27.         enemiesinrange = Physics.OverlapSphere(gameObject.transform.position, explosionradius, enemylayers); //adds anything with the enemylayers into array so only enemies
    28.         foreach (Collider enemy in enemiesinrange)
    29.         {
    30.             Debug.Log("we hit " + enemy.name);
    31.             enemy.GetComponent<Enemy>().TakeDamage(10);//the line that causes my issue
    32.         }
    33.         Destroy(gameObject);
    34.     }
    35.  
    36. }
     
  2. luke_2

    luke_2

    Joined:
    Nov 20, 2012
    Posts:
    29
    I would guess there is not always an 'Enemy' script attached to the 'collision' object and an error is thrown. To check this, enable 'Error Pause' in the console. Or do a check that enemy.GetComponent<Enemy>() is not null before calling TakeDamage.
     
    Cptenglish likes this.
  3. Cptenglish

    Cptenglish

    Joined:
    Apr 24, 2015
    Posts:
    8
    Yeah you were right, i added a check for the enemy.getcomponent and it now works

    thank you very much
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    As an aside, a more-general approach to this which lets you damage many different objects all with the same "TakeDamage(10)" code involves using interfaces.

    Using Interfaces in Unity3D:

    https://forum.unity.com/threads/how...rereceiver-error-message.920801/#post-6028457

    https://forum.unity.com/threads/manager-classes.965609/#post-6286820

    Check Youtube for other tutorials about interfaces and working in Unity3D. It's a pretty powerful combination.