Search Unity

NullReferenceExeption

Discussion in 'Scripting' started by Pirasp, Jul 31, 2015.

  1. Pirasp

    Pirasp

    Joined:
    Jul 31, 2015
    Posts:
    3
    I get the error "NullReferenceExeption" in line 17 in this code, but i cant find it! Help?

    using UnityEngine;
    using System.Collections;

    Code (csharp):
    1. public class shoot : MonoBehaviour {
    2.     public GameObject bullet_prefab;
    3.     public float bulletImpulse = 100f;
    4.     // Use this for initialization
    5.     void Start () {
    6.  
    7.     }
    8.  
    9.     // Update is called once per frame
    10.     void Update () {
    11.  
    12.         if (Input.GetButtonDown ("Fire1")) {
    13.             Camera cam = Camera.current;
    14.             GameObject thebullet = (GameObject)Instantiate (bullet_prefab, cam.transform.position + cam.transform.forward, cam.transform.rotation);
    15.             thebullet.GetComponent<Rigidbody>().AddForce(cam.transform.forward * bulletImpulse, ForceMode.Impulse);
    16.         }
    17.     }
    18. }
    edit: sorry for no code tags...
     
    Last edited: Jul 31, 2015
    Master-Frog likes this.
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    [code ][/code ] tags please.

    Also, use Camera.main and not Camera.current. Do you actually have a prefab assigned to the bullet_prefab in the inspector? And does the GameObject in the prefab actually have a Rigidbody assigned to it?
     
    martinmr likes this.
  3. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    can you edit it as code
    [code ]

    [/code ]
     
    Pirasp likes this.
  4. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Code (csharp):
    1. public GameObject bullet_prefab;
    Code (csharp):
    1. thebullet.GetComponent<Rigidbody>()
    Are meant to be the same prefab?

    And please use code tags as asked next time.
     
  5. Pirasp

    Pirasp

    Joined:
    Jul 31, 2015
    Posts:
    3
    Camera.current is needed, because its part of a multiplayer game, and i would be shooting multiple projectiles otherwise, and yes I have essigned a prefab...
     
  6. Pirasp

    Pirasp

    Joined:
    Jul 31, 2015
    Posts:
    3
    no, they are not
     
  7. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    so in which line is your null reference exception now, looking at your code line 17 is empty
     
  8. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Code (csharp):
    1. public class shoot : MonoBehaviour
    2. {
    3.    public GameObject bullet_prefab;
    4.    public float bulletImpulse = 100f;
    5.  
    6.    void Update ()
    7.    {
    8.        if(Input.GetButtonDown ("Fire1"))
    9.        {
    10.            if(bullet_prefab == null)
    11.               Debug.Log("null prefab");
    12.  
    13.            Camera cam = Camera.current;
    14.  
    15.            if(cam == null)
    16.               Debug.Log("null camera");
    17.            
    18.            GameObject theBullet = GameObject.Instantiate(bullet_prefab, cam.transform.position + cam.transform.forward, cam.transform.rotation);
    19.  
    20.             if(theBullet == null)
    21.                Debug.Log("null bullet");
    22.  
    23.             Rigidbody bulletRigidbody = theBullet.GetComponent<Rigidbody>();
    24.  
    25.             if(bulletRigidbody == null)
    26.                Debug.Log("null rigidbody");
    27.  
    28.             bulletRigidbody.AddForce(cam.transform.forward * bulletImpulse, ForceMode.Impulse);
    29.        }
    30.    }
    31. }
    Welcome to the world of debugging.
     
    martinmr likes this.
  9. Master-Frog

    Master-Frog

    Joined:
    Jun 22, 2015
    Posts:
    2,302
    Ahh... I'll just say, a null reference exception basically means that you're trying to push a button on a device that doesn't exist. Make sure when you tell a computer to press a button that the button exists, or they get very angry.