samedi 5 septembre 2020

My if statement Functions doesn't work at all

I am just making 2d space shooting game from this video. (https://www.youtube.com/watch?v=2mNXfrh0UYo&t=2720s)I made a script that and when enemy's projectile was collided to the player spaceship ,destroy player's spaceship by researching tags of players, but when I run the game, the player spaceship doesn't destroy at all. (I already checked tags on Player's spaceship, but I already put Player tags on Player's spaceship.) I tried to put Debug.Log in the OnCollisionEnter2D method to find where I got wrong, but When I try to run the game, I found that the function of all OnCollisionEnter function for the Player doesn't work(I also tried to put BoxCollider2D on every prefabs and turn "Is Trigger" to solve this problem, but it was not right solution for that). From these things, The problem I got can be said that problem is The if statements and researching tags function in my code is not working at all. Is there any possible problems left and way to solve that problems?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyProjectile : MonoBehaviour
{
    public GameObject Projectile_Enemy;
    public float speed;

    // Start is called before the first frame update
    void Start() 
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(new Vector3(0, -1 * speed * Time.deltaTime, 0));
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            Destroy(collision.gameObject); // Problem Is here, this if statements doesn't work at all.
            Debug.Log("test");
            Destroy(Projectile_Enemy);
            Debug.Log("Test");
        }
        if (collision.gameObject.tag == "Wall")
        {
            Destroy(Projectile_Enemy);
            Debug.Log("TEST");
        }
    }
}

The Unity version I use is Unity 2019 3 14 and I edit my code by Microsoft Visual Studio.

Aucun commentaire:

Enregistrer un commentaire