O
O
O2topGG2020-08-04 18:26:31
Unity
O2topGG, 2020-08-04 18:26:31

Is an object not removed when it collides with another?

using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;

public class Bird : MonoBehaviour
{
    public float force;
    Rigidbody2D BirdRigid;
    void Start()
    {
        BirdRigid = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0)) { 
        BirdRigid.velocity = Vector2.up * force;
        }
    }

    private void OnCollisionEnter2D(Collision2D collision) 
    {
        if(collision.collider.tag == "Enemy") 
        {
            Destroy(gameObject);
        }
    }
}
Вот код

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zZaKko, 2020-08-04
@zZaKko

You should take in collision gameobject, not collider.

private void OnCollisionEnter2D(Collision2D collision) 
    {
        if(collision.gameobject.tag == "Enemy") //вот так
        {
            Destroy(gameObject);
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question