Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Gun and Target not working, please help [ANSWERED BELOW]

Discussion in 'Scripting' started by Rocketman03, May 28, 2018.

  1. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    I have been following this code by Brackeys and for the past 8 hours I have not been able to solve it. It won't work but it used to work until version 2018. Please help
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Gun : MonoBehaviour {
    6.  
    7.     public float damage = 10f;
    8.     public float range = 100f;
    9.  
    10.     public GameObject fpsCam;
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.        
    15.  
    16.         if (Input.GetButtonDown("Fire1"))
    17.         {
    18.             Shoot();
    19.         }
    20.  
    21.     }
    22.  
    23.     public void Shoot ()
    24.     {
    25.         RaycastHit hit;
    26.         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range));
    27.         {
    28.             Target target = hit.transform.GetComponent<Target>();
    29.             if (target != null)
    30.             {
    31.                 target.TakeDamage(damage);
    32.             }
    33.         }
    34.     }
    35. }
    That is the code for the Gun and the following is the target.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Target : MonoBehaviour {
    6.  
    7.     public float health = 50f;
    8.  
    9.     public void TakeDamage(float amount)
    10.     {
    11.         health -= amount;
    12.         if (health <= 0f)
    13.         {
    14.             Die();
    15.         }
    16.     }
    17.  
    18.     void Die ()
    19.     {
    20.         Destroy(gameObject);
    21.     }
    22. }
    23.  
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Do you get any errors?
    If not, put a debug in the update and see if it is picking up the GetButtonDown.
     
    Rocketman03 likes this.
  3. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    Yeah I get "Null Reference Exception: Object Reference not set to instance of an object"
     
  4. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    I did that, but it didn't work
     
  5. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    It will also give you the line number where the null reference occurred, which is needed to figure out which one it is.
     
  6. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
  7. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    That's probably because you are hitting something that doesn't have a Target script attached. Put a debug here:
    Code (csharp):
    1.  
    2. [LIST=1]
    3. [*]            if (target != null)
    4. [*]            {
    5. [*]                target.TakeDamage(damage);
    6. [*]                Debug.Log("target message sent");
    7. [*]            }
    8. [/LIST]
    9.  
    Then make sure you are firing on the target at a close enough range, and see if you get the Debug print.
     
  8. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    Okay, I will test it
     
  9. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    I tested it, and it still doesn't work. I get the same error. I am thinking it could be a bug in unity as this code has always worked before flawless
     
  10. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    And it does a have the "Target" script attached
     
  11. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Try this:
    Be sure to comment out the other code and then add the debug
    1. {
    2. // Target target = hit.transform.GetComponent<Target>();
    3. // if (target != null)
    4. // {
    5. // target.TakeDamage(damage);
    6. // }
    7. Debug.Log(hit.transform.gameObject.name);
    8. }
     
  12. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    Okay, that did work
     
  13. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Did it print the name of the target object?
     
  14. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    Yes
     
  15. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    and now it does not work, It printed the name of the gameobject, but now it is not working and gives me the same error
     
  16. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Is it pointing to the hit line with the debug on it?
     
  17. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    its pointing to the line with Debug.Log(hit.transform.gameObject.name);
     
  18. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    then just try:
    Debug.Log(hit.transform);
     
  19. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    That worked, even if I restart it. It prints out "Null"
     
  20. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Paste the shoot function again, with the changes.
     
  21. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    Code (CSharp):
    1. public void Shoot ()
    2.     {
    3.         RaycastHit hit;
    4.         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range));
    5.         {
    6.            // Target Target = hit.transform.GetComponent<Target>();
    7.            // if (Target != null)
    8.             //{
    9.                // Target.TakeDamage(damage);
    10.                 Debug.Log("target message sent");
    11.             Debug.Log(hit.transform);
    12.  
    13.  
    14.             // }
    15.         }
    16.     }
    17. }
     
  22. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Take the semicolon off of this line at the end:
    if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range));

    Then try it again.
     
  23. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    should I put back the Target function?
     
  24. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    I did, and everything inside RaycastHit hit; did not function. Other than that it it worked
     
  25. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Paste what it looks like now.
     
  26. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Gun : MonoBehaviour {
    6.  
    7.     public float damage = 10f;
    8.     public float range = 100f;
    9.  
    10.     public GameObject fpsCam;
    11.    
    12.     // Update is called once per frame
    13.     void FixedUpdate () {
    14.        
    15.  
    16.         if (Input.GetButtonDown("Fire1"))
    17.         {
    18.             Shoot();
    19.             Debug.Log("i shot the gun");
    20.         }
    21.  
    22.     }
    23.  
    24.     public void Shoot ()
    25.     {
    26.         RaycastHit hit;
    27.         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
    28.         {
    29.            // Target Target = hit.transform.GetComponent<Target>();
    30.            // if (Target != null)
    31.             //{
    32.                // Target.TakeDamage(damage);
    33.                 Debug.Log("target message sent");
    34.             Debug.Log(hit.transform);
    35.  
    36.  
    37.             // }
    38.         }
    39.     }
    40. }
     
  27. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Put the
    Debug.Log(hit.transform.gameObject.name);
    Then run it and see if it prints the object name.
     
  28. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    It didn't work, it only prints Debug.Log("i shot the gun");
     
  29. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You did a save before trying?
    Check if the object has a collider on it.
    Otherwise, drop a cube with a collider in the scene and try firing at it, making sure it has a collider.
     
  30. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    Yes, I saved it. I think its a bug in Unity. Sorry If I am wasting your time sir or ma'am
     
  31. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    and they do have colliders
     
  32. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    They would have checked something like that before putting it out there because it's so common. How big is your project? Can you strip it down to just a small project with a cube and post it somewhere? My guess is that it's just not hitting the object for some reason.
     
  33. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    It is very small, just 2 cubes and a terrain and the first person controller and the gun. I will post it.
     
  34. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    what is your email? I can probably email the project to you
     
  35. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
  36. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I'll PM my email.
     
  37. Rocketman03

    Rocketman03

    Joined:
    Jul 14, 2017
    Posts:
    23
    ANSWER: Use a camera instead of a gameobject for FpsCam