I Have a problem...

As the title says,i have a problem with my scripts.Probably I can troubleshoot it easly but the problem is,I cant find the problem.Maybe you guys can help me?I guess I want a lot of thing but if I describe good,you guys can help.

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class DamagedByCollision : MonoBehaviour
{

    public int health = 1;
   

    public float invulnPeriod = 0;
    float invulnTimer = 0;
    int correctLayer;
    public Transform player;
    public Transform end;
    public Text scoreText;
    public Transform enemy;
   

    SpriteRenderer spriteRend;

    void Start()
    {
        correctLayer = gameObject.layer;


        spriteRend = GetComponent<SpriteRenderer>();

        if (spriteRend == null)
        {
            spriteRend = transform.GetComponentInChildren<SpriteRenderer>();

            if (spriteRend == null)
            {
                Debug.LogError("Object '" + gameObject.name + "' has no sprite renderer.");
            }
        }
    }

    void OnTriggerEnter2D(Collider2D hit)
    {


        if(end.gameObject.tag == "End")
        {
            health++;

            if (invulnPeriod > 0)
            {
                invulnTimer = invulnPeriod;
                gameObject.layer = 11;
            }


           

        }

       


       



        if (player.gameObject.name == "Air Ship")
        {

            health--;

            if (invulnPeriod > 0)
            {
                invulnTimer = invulnPeriod;
                gameObject.layer = 11;
            }

        }

       
        if(hit.gameObject.name == "Air Ship")
        {

             Scene CS = SceneManager.GetActiveScene();

                 SceneManager.LoadScene(CS.name);

           


        }

       



    }

    void Update()
    {





        if (invulnTimer > 0)
        {
            invulnTimer -= Time.deltaTime;

            if (invulnTimer <= 0)   
            {
                gameObject.layer = correctLayer;
                if (spriteRend != null)
                {
                    spriteRend.enabled = true;
                }
            }
            else
            {
                if (spriteRend != null)
                {
                    spriteRend.enabled = !spriteRend.enabled;
                }
            }
        }


       
       

            if (health <= 0)
            {
                Die();



            }


       

           
           

       
       
       
    }

    void Die()
    {
        Destroy(gameObject);
    }





   


}

This is the script that probably making problems.I have a shooting mechanic in my game.It was working fine until today.Bullets are passing through the enemies.If you need more details I am here.

5984546--643187--SS.png

Run the game, fire some bullets, then stop the game using PAUSE in the editor and inspect everything. Are the layers correct? Are all scripts in place? Use Debug.Log() to make sure code is actually running and to make sure any values (such as timers and booleans) are what you expect them to be. Step the game one step at a time in the inspector, see what’s happening.

1 Like

Thank you!!Because of me being stupid I forgot to add a “If” that will allow the enemy to get damage.Thank you again.

Another problem that might exist is if you’re using collision to detect if your “shooting” and enemy. Unity’s physics engine runs at a fixed framerate. It could be that inbetween frames your object passes through your enemies, therefore “ignoring” collision. You can mitigate this by using some different methods.

One is to use hitscanning instead. There’s plenty of material out there to find what you’d need.

And the other is to raytrace in front of your bullets to make sure you won’t accidentally pass through objects without having collision detection going off.