Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bullet's not harming player

Discussion in 'Scripting' started by S3Critsss, Jan 24, 2020.

  1. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    I'm making a kind of top down shooter but when my bullet hit's the other player's it doesn't harm them and i've made sure that the bullet's collider is set to trigger but its still doesn't work i dont know if it is becuase i added multiplayer to the game but here are the script

    Bullet Script
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class BulletScript : MonoBehaviour
    7. {
    8.     // public
    9.  
    10.     public float bulletTravelSpeed = 7f;
    11.     public float maxDist;
    12.     public float CurrentDist;
    13.     public float damage;
    14.  
    15.     // private
    16.  
    17.     public GameObject triggeringEnemy;
    18.  
    19.     // void
    20.  
    21.     void Update()
    22.     {
    23.         transform.Translate(Vector3.forward * Time.deltaTime * bulletTravelSpeed);
    24.         CurrentDist += 1 * Time.deltaTime;
    25.  
    26.         if (CurrentDist >= maxDist)
    27.         {
    28.             Destroy(this.gameObject);
    29.         }
    30.     }
    31.  
    32.     public void OnTriggerEnter(Collider other)
    33.     {
    34.         if (other.tag == "Player")
    35.         {
    36.             triggeringEnemy = other.gameObject;
    37.             triggeringEnemy.GetComponent<Enemy>().Health -= damage;
    38.         }
    39.     }
    40. }
    41.  
    42.  
    Enemy Script
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Enemy : MonoBehaviour
    7. {
    8.     // public
    9.  
    10.     public float Health;
    11.  
    12.     // private
    13.  
    14.  
    15.  
    16.     // void
    17.  
    18.     public void Update()
    19.     {
    20.         if (Health <= 0)
    21.         {
    22.             Die();
    23.         }
    24.     }
    25.  
    26.     public void Die()
    27.     {
    28.         Destroy(gameObject);
    29.     }
    30.  
    31. }
    32.  
    33.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If it is networked multiplayer, I'm sure that's an issue, as there is nothing networked multiplayer in this code.

    But assuming you meant local multiplayer, add Debug.Log statements to your OnTriggerEnter. Verify it is first being called, and by what object(s). Then verify the tag comparison is returning true. Lastly verify you are getting zero runtime errors logged, as line 37 in your bullet script could be producing null reference errors since you're not checking if GetComponent is returning null before you're using it.
     
  3. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    I'm not sure but it's the one where you use network manager and you don't need scripting. and i added a debug.Log to the OnTriggerEnter but nothing comes up
     
    Last edited: Jan 24, 2020
  4. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    I just changed the Tag of the bullet to Player and it started coming up with null references so i changed it back do i have to do something to line 37 cause that's where the error was how can i fix it
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Well the bullet itself shouldn't be Player. The way your script is written it is expecting the object which the bullet hits to be the one with that tag, not itself with that tag. A null reference on line 37 means the GameObject the bullet hit that is tagged with Player does not have an "Enemy" component. If your Player object doesn't have an Enemy component, that would be the problem causing the null reference exception.
     
  6. VSMGames

    VSMGames

    Joined:
    Jan 12, 2020
    Posts:
    47
    Also make sure that one of your game objects, bullet or player has a rigidbody component attached.
     
    Joe-Censored likes this.
  7. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    The player does have enemy script
     
  8. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    Player and bullet have rigidbodies
     
  9. VSMGames

    VSMGames

    Joined:
    Jan 12, 2020
    Posts:
    47
    Try to do the damage calculation on the player inside the Enemy script. If that works then you can make the bullet carry only the damage done when hitting the player.
     
  10. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    How would i do this (I'm new to coding sorry)
     
  11. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If the object with Player tag doesn't have Enemy script your code will always null reference error on line 37. I'd assume you have some other script which tracks the player's health then. Maybe you meant to GetComponent that instead.