Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question I Have a problem...

Discussion in 'Scripting' started by Hiktses, Jun 15, 2020.

  1. Hiktses

    Hiktses

    Joined:
    May 30, 2020
    Posts:
    19
    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.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4. using UnityEngine.UI;
    5. public class DamagedByCollision : MonoBehaviour
    6. {
    7.  
    8.     public int health = 1;
    9.    
    10.  
    11.     public float invulnPeriod = 0;
    12.     float invulnTimer = 0;
    13.     int correctLayer;
    14.     public Transform player;
    15.     public Transform end;
    16.     public Text scoreText;
    17.     public Transform enemy;
    18.    
    19.  
    20.     SpriteRenderer spriteRend;
    21.  
    22.     void Start()
    23.     {
    24.         correctLayer = gameObject.layer;
    25.  
    26.  
    27.         spriteRend = GetComponent<SpriteRenderer>();
    28.  
    29.         if (spriteRend == null)
    30.         {
    31.             spriteRend = transform.GetComponentInChildren<SpriteRenderer>();
    32.  
    33.             if (spriteRend == null)
    34.             {
    35.                 Debug.LogError("Object '" + gameObject.name + "' has no sprite renderer.");
    36.             }
    37.         }
    38.     }
    39.  
    40.     void OnTriggerEnter2D(Collider2D hit)
    41.     {
    42.  
    43.  
    44.         if(end.gameObject.tag == "End")
    45.         {
    46.             health++;
    47.  
    48.             if (invulnPeriod > 0)
    49.             {
    50.                 invulnTimer = invulnPeriod;
    51.                 gameObject.layer = 11;
    52.             }
    53.  
    54.  
    55.            
    56.  
    57.         }
    58.  
    59.        
    60.  
    61.  
    62.        
    63.  
    64.  
    65.  
    66.         if (player.gameObject.name == "Air Ship")
    67.         {
    68.  
    69.             health--;
    70.  
    71.             if (invulnPeriod > 0)
    72.             {
    73.                 invulnTimer = invulnPeriod;
    74.                 gameObject.layer = 11;
    75.             }
    76.  
    77.         }
    78.  
    79.        
    80.         if(hit.gameObject.name == "Air Ship")
    81.         {
    82.  
    83.              Scene CS = SceneManager.GetActiveScene();
    84.  
    85.                  SceneManager.LoadScene(CS.name);
    86.  
    87.            
    88.  
    89.  
    90.         }
    91.  
    92.        
    93.  
    94.  
    95.  
    96.     }
    97.  
    98.     void Update()
    99.     {
    100.  
    101.  
    102.  
    103.  
    104.  
    105.         if (invulnTimer > 0)
    106.         {
    107.             invulnTimer -= Time.deltaTime;
    108.  
    109.             if (invulnTimer <= 0)  
    110.             {
    111.                 gameObject.layer = correctLayer;
    112.                 if (spriteRend != null)
    113.                 {
    114.                     spriteRend.enabled = true;
    115.                 }
    116.             }
    117.             else
    118.             {
    119.                 if (spriteRend != null)
    120.                 {
    121.                     spriteRend.enabled = !spriteRend.enabled;
    122.                 }
    123.             }
    124.         }
    125.  
    126.  
    127.        
    128.        
    129.  
    130.             if (health <= 0)
    131.             {
    132.                 Die();
    133.  
    134.  
    135.  
    136.             }
    137.  
    138.  
    139.        
    140.  
    141.            
    142.            
    143.  
    144.        
    145.        
    146.        
    147.     }
    148.  
    149.     void Die()
    150.     {
    151.         Destroy(gameObject);
    152.     }
    153.  
    154.  
    155.  
    156.  
    157.  
    158.    
    159.  
    160.  
    161. }
    162.  




    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.
     

    Attached Files:

    • SS.png
      SS.png
      File size:
      10.2 KB
      Views:
      306
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    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.
     
    Hiktses likes this.
  3. Hiktses

    Hiktses

    Joined:
    May 30, 2020
    Posts:
    19
    Thank you!!Because of me being stupid I forgot to add a "If" that will allow the enemy to get damage.Thank you again.
     
  4. Alan-Hart

    Alan-Hart

    Joined:
    Oct 17, 2012
    Posts:
    2
    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.