Search Unity

Gun and Particle System Error. Null Reference Exception: Object.

Discussion in 'Scripting' started by WingNova, Aug 10, 2016.

  1. WingNova

    WingNova

    Joined:
    Aug 10, 2016
    Posts:
    7
    I am having a Error of "NullReferenceException: Object reference not set to an instance of an object Assets/Script/BulletSpawn.cs line 28". So what I want to do is have a Particle System add some more visual effects to the gun shooting but when the Particle System is Instantiate (line 28) it just stays at the locations of spawn and doesn't stay with the the "barrelEnd". Also the variable "hold" is not storing the Instantiate Particle System and therefore I cant set its parent to the barrelEnd. I then cant destroy it on button releases. The end result is that the gun makes a lot of Particle Systems that do not move with the gun and do not get destroyed. Please can anyone help?


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BulletSpawn : MonoBehaviour {
    5.  
    6.     public Rigidbody bullet; // the empty gameobjects that are fired with a trail renderder on them
    7.     public Transform barrelEnd;// place where the bullets come from
    8.     public float rate_of_fire = 0.2f; // rate of fire
    9.     private float time; //count down timer till next fire
    10.     public ParticleSystem bulletTrails; // the added effect so that the bullets are more visible
    11.     private GameObject hold; // hold the Instantiate ParticleSystem "bulletTrails" till the fire button is released then gets destroyed
    12.  
    13.     void Start()
    14.     {
    15.         time = rate_of_fire;  // sets fire rate to the count down
    16.     }
    17.     void FixedUpdate()
    18.     {
    19.         time = time - Time.fixedDeltaTime;//count down
    20.         if (time < 0)//timer till next bullet is fired
    21.         {
    22.             time = rate_of_fire; //timer till next bullet is fired
    23.             if (Input.GetButton("Fire1"))// pressing down the fire button
    24.             {
    25.                 if (hold == null)// seems to think that hold is always null???? check to see if one is already created
    26.                 {
    27.                     hold = Instantiate(bulletTrails, barrelEnd.position, barrelEnd.rotation) as GameObject; // creates the partical effect so the bullet can be shown
    28.                     barrelEnd = hold.transform.parent; // error code, says null reference exception???? trying to set parent to barrelEnd so the partical Systems stays with the gun
    29.                 }
    30.                 Rigidbody bulletInstance = Instantiate(bullet, barrelEnd.position, barrelEnd.rotation) as Rigidbody; // creates the real bullets to hit objects
    31.                 bulletInstance.AddForce(barrelEnd.forward * 5000);// adds the force to the bullet
    32.             }
    33.             if (Input.GetButtonUp("Fire1"))//fire button up
    34.             {
    35.                Destroy(hold); //not working??? destroys the partical system so that there isnt more then one partical system going off
    36.             }
    37.         }
    38.     }
    39.  
    40. }
    41.  
    42.  
     
  2. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    "hold" is always null because you're trying to instantiate a Component and not a Prefab or GameObject. Is there a reason you can't instantiate a bullet trail prefab in Awake or Start and simply enable or disable it depending on the state of the fire button? You should also never poll input in FixedUpdate because there is a high chance you will get dropped input.
     
  3. WingNova

    WingNova

    Joined:
    Aug 10, 2016
    Posts:
    7
    I didn't have a problem instantiate it. I put the Prefab in the editor for the value of "bulletTrails". I just fixed the problem. Here is the Fixed code. I had to instantiate as a particle system and make "hold" a particle system also. I would have just turned on and off the particle system prefab but I couldn't get it to work so I just make it and destroy it because there will be very few of them in the game. Thanks for the advice of not putting input in FixedUpdate. I am thinks of putting it in a
    coroutine.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BulletSpawn : MonoBehaviour {
    5.  
    6.     public Rigidbody bullet; // the empty gameobjects that are fired with a trail renderder on them
    7.     public Transform barrelEnd;// place where the bullets come from
    8.     public float rate_of_fire = 0.2f; // rate of fire
    9.     private float time; //count down timer till next fire
    10.     public ParticleSystem bulletTrails; // the added effect so that the bullets are more visible
    11.     private ParticleSystem hold; // hold the Instantiate ParticleSystem "bulletTrails" till the fire button is released then gets destroyed
    12.     private int current_Particle = 0;
    13.  
    14.     void Start()
    15.     {
    16.         time = rate_of_fire;  // sets fire rate to the count down
    17.     }
    18.     void FixedUpdate()
    19.     {
    20.         time = time - Time.fixedDeltaTime;//count down
    21.         if (time < 0)//timer till next bullet is fired
    22.         {
    23.             time = rate_of_fire; //timer till next bullet is fired
    24.             if (Input.GetButton("Fire1"))// pressing down the fire button
    25.             {
    26.                 if (hold == null && current_Particle == 0)// check to see if one is already created
    27.                 {
    28.                     current_Particle = 1; //tells that it has creating one
    29.                     hold = (ParticleSystem)Instantiate(bulletTrails, barrelEnd.position, barrelEnd.rotation)as ParticleSystem; // creates the partical effect so the bullet can be shown
    30.                     hold.transform.parent = barrelEnd; //sets parent to barrelEnd so the partical Systems stays with the gun
    31.                 }
    32.                 Rigidbody bulletInstance = Instantiate(bullet, barrelEnd.position, barrelEnd.rotation) as Rigidbody; // creates the real bullets to hit objects
    33.                 bulletInstance.AddForce(barrelEnd.forward * 5000);// adds the force to the bullet
    34.             }
    35.            
    36.         }
    37.         if (Input.GetButtonUp("Fire1"))//fire button up
    38.         {
    39.             if (hold != null)
    40.             {
    41.                 Destroy(hold.gameObject); //destroys the partical system so that there isnt more then one partical system going off
    42.                 current_Particle = 0; // sets the counter back to 0
    43.             }
    44.         }
    45.     }
    46.  
    47. }
    48.  
     
  4. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    I don't think you understand... ParticleSystem is a component, you cannot instantiate components. Why is the bullet trail attached to the end of the barrel in the first place? You're also trying to instantiate a Rigidbody component(bullet) which will not work. Change the type to GameObject and use Update instead of FixedUpdate.

    It would be significantly easier if you just make 2 scripts. One for the bullet behaviour and one for the gun behaviour..

    Attach this to a Bullet prefab and add the bullet trail ParticleSystem to the prefab as well:
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(Rigidbody))]
    4. public class BulletBehaviour : MonoBehaviour
    5. {
    6.    public Rigidbody Rigidbody;
    7.  
    8.    void Awake()
    9.    {
    10.      Rigidbody = GetComponent<Rigidbody>();
    11.    }
    12.  
    13.    void Update()
    14.    {
    15.      Rigidbody.AddForce(transform.forward * 5000 * Time.deltaTime);// adds the force to the bullet
    16.    }
    17. }
    Attach this to a Gun prefab and assign the BulletPrefab:
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class GunBehaviour : MonoBehaviour
    4. {
    5.    public GameObject BulletPrefab; // the bullet prefab to instantiate
    6.  
    7.    public Transform BarrelEnd; // place where the bullets come from
    8.    public float FireRate = 0.2f; // rate of fire
    9.    public float NextFire = 0.0f; // next fire time
    10.  
    11.    void Update()
    12.    {
    13.      if (Input.GetButton("Fire1"))
    14.      {
    15.        Fire();
    16.      }
    17.    }
    18.  
    19.    public void Fire()
    20.    {
    21.      if (Time.time >= NextFire)
    22.      {
    23.        NextFire = Time.time + FireRate;
    24.  
    25.        Instantiate(BulletPrefab, BarrelEnd.position, BarrelEnd.rotation);
    26.      }
    27.    }
    28. }
     
    Last edited: Aug 10, 2016