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

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?

using UnityEngine;
using System.Collections;

public class BulletSpawn : MonoBehaviour {

    public Rigidbody bullet; // the empty gameobjects that are fired with a trail renderder on them
    public Transform barrelEnd;// place where the bullets come from
    public float rate_of_fire = 0.2f; // rate of fire
    private float time; //count down timer till next fire
    public ParticleSystem bulletTrails; // the added effect so that the bullets are more visible
    private GameObject hold; // hold the Instantiate ParticleSystem "bulletTrails" till the fire button is released then gets destroyed

    void Start()
    {
        time = rate_of_fire;  // sets fire rate to the count down
    }
    void FixedUpdate()
    {
        time = time - Time.fixedDeltaTime;//count down
        if (time < 0)//timer till next bullet is fired
        {
            time = rate_of_fire; //timer till next bullet is fired
            if (Input.GetButton("Fire1"))// pressing down the fire button
            {
                if (hold == null)// seems to think that hold is always null???? check to see if one is already created 
                {
                    hold = Instantiate(bulletTrails, barrelEnd.position, barrelEnd.rotation) as GameObject; // creates the partical effect so the bullet can be shown
                    barrelEnd = hold.transform.parent; // error code, says null reference exception???? trying to set parent to barrelEnd so the partical Systems stays with the gun
                }
                Rigidbody bulletInstance = Instantiate(bullet, barrelEnd.position, barrelEnd.rotation) as Rigidbody; // creates the real bullets to hit objects
                bulletInstance.AddForce(barrelEnd.forward * 5000);// adds the force to the bullet
            }
            if (Input.GetButtonUp("Fire1"))//fire button up
            {
               Destroy(hold); //not working??? destroys the partical system so that there isnt more then one partical system going off
            }
        }
    }

}

“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.

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.

using UnityEngine;
using System.Collections;

public class BulletSpawn : MonoBehaviour {

    public Rigidbody bullet; // the empty gameobjects that are fired with a trail renderder on them
    public Transform barrelEnd;// place where the bullets come from
    public float rate_of_fire = 0.2f; // rate of fire
    private float time; //count down timer till next fire
    public ParticleSystem bulletTrails; // the added effect so that the bullets are more visible
    private ParticleSystem hold; // hold the Instantiate ParticleSystem "bulletTrails" till the fire button is released then gets destroyed
    private int current_Particle = 0;

    void Start()
    {
        time = rate_of_fire;  // sets fire rate to the count down
    }
    void FixedUpdate()
    {
        time = time - Time.fixedDeltaTime;//count down
        if (time < 0)//timer till next bullet is fired
        {
            time = rate_of_fire; //timer till next bullet is fired
            if (Input.GetButton("Fire1"))// pressing down the fire button
            {
                if (hold == null && current_Particle == 0)// check to see if one is already created 
                {
                    current_Particle = 1; //tells that it has creating one
                    hold = (ParticleSystem)Instantiate(bulletTrails, barrelEnd.position, barrelEnd.rotation)as ParticleSystem; // creates the partical effect so the bullet can be shown
                    hold.transform.parent = barrelEnd; //sets parent to barrelEnd so the partical Systems stays with the gun
                }
                Rigidbody bulletInstance = Instantiate(bullet, barrelEnd.position, barrelEnd.rotation) as Rigidbody; // creates the real bullets to hit objects
                bulletInstance.AddForce(barrelEnd.forward * 5000);// adds the force to the bullet
            }
           
        }
        if (Input.GetButtonUp("Fire1"))//fire button up
        {
            if (hold != null)
            {
                Destroy(hold.gameObject); //destroys the partical system so that there isnt more then one partical system going off
                current_Particle = 0; // sets the counter back to 0
            }
        }
    }

}

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:

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class BulletBehaviour : MonoBehaviour
{
   public Rigidbody Rigidbody;

   void Awake()
   {
     Rigidbody = GetComponent<Rigidbody>();
   }

   void Update()
   {
     Rigidbody.AddForce(transform.forward * 5000 * Time.deltaTime);// adds the force to the bullet
   }
}

Attach this to a Gun prefab and assign the BulletPrefab:

using UnityEngine;

public class GunBehaviour : MonoBehaviour
{
   public GameObject BulletPrefab; // the bullet prefab to instantiate

   public Transform BarrelEnd; // place where the bullets come from
   public float FireRate = 0.2f; // rate of fire
   public float NextFire = 0.0f; // next fire time

   void Update()
   {
     if (Input.GetButton("Fire1"))
     {
       Fire();
     }
   }

   public void Fire()
   {
     if (Time.time >= NextFire)
     {
       NextFire = Time.time + FireRate;

       Instantiate(BulletPrefab, BarrelEnd.position, BarrelEnd.rotation);
     }
   }
}