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

Bug Top down shooter, Object reference not set to an instance of an object

Discussion in '2D' started by colonel_Yuri, Jun 24, 2021.

  1. colonel_Yuri

    colonel_Yuri

    Joined:
    Jan 5, 2021
    Posts:
    1
    Hello there, I have been developing a top down shooter game (think of SAS4 and Hotline Miami but worse in every way possible), and I have encountered a problem with my code (am still new to this coding stuff).

    NullReferenceException: Object reference not set to an instance of an object
    shooting.Shoot () (at Assets/shooting.cs:31)
    shooting.Update () (at Assets/shooting.cs:22)


    (full code below, version "2018.4.14f1")


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class shooting : MonoBehaviour {
    6.  
    7.    private Transform firePoint;
    8.    private GameObject bulletPreFab;
    9.    private float bulletForce = 20f;
    10.  
    11.     // Start is called before the first frame update
    12.     public void Start()
    13.     {
    14.      
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     public void Update()
    19.     {
    20.         if(Input.GetButtonDown("Fire1"))
    21.        {
    22.            Shoot();
    23.        }
    24.     }
    25.  
    26.    public void Shoot()
    27.    {
    28.        Rigidbody2D rB = new Rigidbody2D();
    29.        //GameObject go = new GameObject();
    30.      
    31.        GameObject.Instantiate(bulletPreFab, firePoint.position, firePoint.rotation);
    32.        //bullet.GetComponent<Rigidbody2D>();
    33.        rB.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
    34.    }
    35.  
    36.    public Transform getFirePoint()
    37.    {
    38.        return firePoint;
    39.    }
    40.  
    41.    public GameObject getBulletPreFab()
    42.    {
    43.        return bulletPreFab;
    44.    }
    45.  
    46.    public float getBulletForce()
    47.    {
    48.        return bulletForce;
    49.    }
    50.  
    51. }
    52.  
     
    Last edited: Jun 24, 2021
  2. M4dR0b

    M4dR0b

    Joined:
    Feb 1, 2019
    Posts:
    108
    Y-hello there,

    welcome to the wonderful world of game devs firstly.

    Secondly, an Object Reference Exceptions basically means that there's something missing. In the message error you posted there's is exactly the pointer to the culprit objects, ergo: bulletPrefab and firePoint.

    To fix it, first declare those 2 variables public like:

    Code (CSharp):
    1.  
    2. public Transform firePoint;
    3. public GameObject bulletPreFab;
    4.  
    Then in the inspector, select the object that have the class shooting and assign those two objects:

    Screenshot 2021-06-24 140259.png

    Hope this helps, happy coding :D