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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Destroying Enemy

Discussion in 'Scripting' started by MisterSkitz, Aug 29, 2018.

  1. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    I have set up an enum function that list the tags of my enemies.

    public enum EnemyType{
    Enemy1,enemy2,enemy3,enemy4,enemy5, boss1,boss2,boss3,boss4,boss5
    }

    Then I preload all enemies with stats pulled from the enemy class. And because I don't want to reveal anymore code than I have to, here's an example of how I do that. (filler code)

    if (gameObject.CompareTag(CurrentEnemyType.ToString()))
    {
    if (CurrentEnemyType == EnemyType.Enemy1) {
    MaxHp =
    Hp = MaxHp;
    Debug.Log(Hp);
    }

    Now my enemy('s) have been populated with random stats across the board. Next, I need to be able to kill the enemy so I created an on trigger event that correlates to my bullet.

    void OnTriggerEnter(Collider other){

    if(other.tag == "bullet"){
    Hp -= 25;
    if(Hp <= 0){

    Destroy();
    }


    }

    And that's where I'm stuck. I don't know how to go about finding the game object that I'm encountering.I would imagine that I need a variable, let's say

    GameObject currentEnemy;

    But even if I do this, how do I get the current enemy into that variable? Once I accomplish this, i can finish the rest of the code. I just need that game object!!!!
     
  2. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    Do you mean that you want to destroy the gameobject that the script i attached to? If so:
    Destroy (gameObject);
     
    Doug_B likes this.
  3. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Yes, that's what I want to do. However, all I've retrieved is the ENEMY TYPE. Which is its tag. What I need is to figure out how to obtain its game object. No two enemies are the same, so each new enemy I encounter needs its game object retrieved. Not just its type.(Which is only used for giving the enemy random stats.)
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I think barskey is meaning to amend the Destroy line in your
    OnTriggerEnter(Collider other)
    . That will destroy the enemy itself.

    Are you asking how, inside the
    OnTriggerEnter
    , to destroy the bullet that dealt the blow to the enemy? If so, then something like
    Destroy( other.gameObject )
    should do it.
     
  5. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    This was my final solution:

    public static GameObject enemyObj;

    void OnTriggerEnter(){


    enemyObj = other.GetComponent<GameObject>(); //Grabs enemy object to pass
    to the enemies class

    }

    My problem was that because there's mo getComponent<GameObject>() so I was confused on how it functions.

    enemyObj = other.gameObject;

    So you guys were right but I was approaching it with the wrong call.
     
    Doug_B likes this.