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. Dismiss Notice

Bug Prefab and bullet not instantiating on a weapon

Discussion in 'Scripting' started by ScottDomini, Feb 1, 2021.

  1. ScottDomini

    ScottDomini

    Joined:
    Mar 31, 2020
    Posts:
    2
    Hey,

    So I'm trying to create a weapon system by using classes and inheriting different scripts from a base one. The problem is that I don't really know how this works (as I'm learning OOP on the way) and I got an error I don't really know how to solve.

    NullReferenceException: Object reference not set to an instance of an object
    Weapon.UseWeapon () (at Assets/Weapon.cs:17)
    Shooting.Update () (at Assets/Shooting.cs:23)

    I have a BasicGun class where I asign its ammo, ammo used on shot, bullet type and bullet force, but when I try to shoot it throws that error. I noticed that the bullet prefab isn't instantiating because it is null, but trying to get the prefab still doesn't work.

    Here's the code:

    Code (CSharp):
    1. public class Weapon : MonoBehaviour
    2. {
    3.     protected Shooting player; // class Shooting is from another script, I may change this later so it makes sense
    4.  
    5.     public int ammo;
    6.     public int ammoRate;
    7.     public GameObject bulletType;
    8.     public float bulletForce;
    9.  
    10.     public Weapon() {  }
    11.  
    12.     public void UseWeapon() {
    13.         GameObject bullet = Instantiate(bulletType, player.firePoint.position, player.firePoint.rotation);
    14.         Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
    15.         rb.AddForce(player.firePoint.up * bulletForce, ForceMode2D.Impulse);
    16.         ammo -= ammoRate;
    17.     }
    18. }
    19.  
    20. public class BasicGun : Weapon
    21. {
    22.  
    23.     private void Awake()
    24.     {
    25.         bulletType = GetComponent<GameObject>();
    26.     }
    27.     public BasicGun()
    28.     {
    29.         ammo = 5;
    30.         ammoRate = 1;
    31.         bulletForce = 45f;
    32.     }
    33. }
    And here's part of the code on the Shooting class
    Code (CSharp):
    1. public Weapon currentWeapon;
    2.  
    3. private void Start()
    4.     {
    5.         currentWeapon = gameObject.AddComponent<BasicGun>();
    6.     }
    7.  
    8. void Update()
    9.     {
    10.        // Debug.Log(currentWeapon == null);
    11.         HandleMouse();
    12.         if (Input.GetButtonDown("Fire1") && currentWeapon.ammo > 0) currentWeapon.UseWeapon();
    13.     }
    prefab_error_unity.png

    Can anyone help me?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    What's on line 17 of Weapon.cs? That's where the error is being thrown.
     
  3. ScottDomini

    ScottDomini

    Joined:
    Mar 31, 2020
    Posts:
    2
    This
    Code (CSharp):
    1. GameObject bullet = Instantiate(bulletType, player.firePoint.position, player.firePoint.rotation);
    I skipped the first 3 lines "using ..." so that didn't count.
    Also, line 23 on Shooting.cs is:
    Code (CSharp):
    1. if (Input.GetButtonDown("Fire1") && currentWeapon.ammo > 0) currentWeapon.UseWeapon();
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'm guessing from your description it is the Instantiate line which is failing (whatever line 17 is). Where exactly are you assigning the reference to "player"?

    But how you troubleshoot null reference errors is always the same:

    1) Go to the line the error refers to
    2) Find all references used on that line
    3) If more than 1 reference used, then before that line check each reference whether it is null
    4) Now that you know which specific reference is null, now go figure out why you're not assigning a reference to it before trying to use it.
     
    Terraya likes this.
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Seems like either
    player
    is null or
    player.firePoint
    is null. It doesn't seem like you assign a value to
    player
    anywhere so seems likely to be that.

    To fix this exception - figure out what is null. Then figure out why it's null and fix it.
     
    Terraya and Joe-Censored like this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    As Praetor points out above, the answer is always the same... ALWAYS. It is the single most common error ever. Don't waste your life on this problem. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
    Terraya likes this.