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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Function Problems

Discussion in 'Scripting' started by Deleted User, Jul 9, 2015.

  1. Deleted User

    Deleted User

    Guest

    I am following the "Brackeys make a game tutorial" and I've come across a problem with my enemy's die on hit script. Basicly, I want to call a function from another script, to destroy the game object when enemy is killed. But for some reason, Uniy gives me a error saying:

    BCE0020: An instance of type 'Enemy' is required to access non static member 'Die'.

    Here is the scripts:

    Enemy:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. function Die ()
    4. {
    5.  
    6. Destroy(gameObject);
    7.  
    8. }

    Die On Hit:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. function OnTriggerEnter ()
    4. {
    5.  
    6. var enemy = transform.GetComponentsInParent (Enemy);
    7. Enemy.Die();
    8.  
    9. }

    Thanks for your help Unity community. ;)
     
  2. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Write enemy.Die() instead of Enemy.Die(). You're calling Die() in your class instead of the object.
     
  3. Deleted User

    Deleted User

    Guest

    Now I see a different error:

    BCE0019: 'Die' is not a member of 'UnityEngine.Component[]'.

    That's strange... :confused:
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Remove the s from GetComponentsInParent
     
  5. massey_digital

    massey_digital

    Joined:
    Apr 28, 2013
    Posts:
    94
    This is how I would write the script: (In C#)

    EnemyDeath.cs

    public class EnemyDeath : Monobehaviour
    {
    public void OnTriggerEnter(Collider collider)
    {
    if(collider.gameObject.tag=="Enemy")
    {
    Destroy(collider.gameObject);
    }
    }

    }

    *Make sure to set the tag of your Enemy to "Enemy"
    **I also wouldn't use Destroy. Instead I would use Object Pooling (gameObject.SetActive(true/false)
     
  6. Deleted User

    Deleted User

    Guest

    ohhh.... Thanks, I make that mistake a lot. :oops: