S
S
Sergey Orobinsky2019-03-17 08:39:23
Unity
Sergey Orobinsky, 2019-03-17 08:39:23

What to do if Collision2D doesn't work?

here is the script of 1 object

using UnityEngine;
using System.Collections;
public class EnemyScript : MonoBehaviour
{
   
    public int health = 2;
    
    public GameObject boom;
   
    public int damage = 1;
    void Start()
    {
        boom = GameObject.Find("Boom");
    }
    void OnTriggerEnter2D(Collider2D collider)
    {
        
        if (collider.gameObject.tag == "Bullet")
        {
            GetComponent<AudioSource>().Play();
            health = health - 1;

        }
        if (health <= 0)
        {
            GameObject.Find("EnemySpawn").GetComponent<GameController>().KilledEnemy();
            
            boom.transform.position = new Vector2(transform.position.x, transform.position.y);
            boom.GetComponent<ParticleSystem>().Play();
            boom.GetComponent<AudioSource>().Play();
            Destroy(gameObject);

        }
    }
}

and the second script
using UnityEngine;
using System.Collections;

public class Bullet : MonoBehaviour
{

   
    public float lifetime = 2.0f;

    
    public float speed = 5.0f;

    
    public int damage = 1;

    // Use this for initialization
    void Start()
    {
        
        Destroy(gameObject, lifetime);
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector3.up * Time.deltaTime * speed);
    }
    void OnTriggerEnter2D(Collider2D collider)
    {
        
        if (collider.gameObject.tag == "Enemy")
        {
            Destroy(gameObject);

        }
        
    }
}

and what's wrong?
5c8ddcc4f4055689820494.png5c8ddd4bb35b4179839084.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sir_Akakii, 2019-03-18
@serg_orobik

For triggers to fire, one of these objects must have a Rigidbody2D component

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question