Bulletshells not working as intended

Hi!
I’m working on a TopDown-Shooter. Shooting works fine. Now I want to implement bullet shells, which pop out of the gun everytime the “Fire”-Button is pressed (it’s an Autorifle). I want the shells to fall on the ground, lay there for a certain time and disappear after a while.
This is my code so far:

public float fireRate;
	private float nextFire;
	private GameObject InstantiatedObj;
	public GameObject shot;
	public Transform shotSpawn;
	public Transform MuzzleFlashPrefab;
	public Transform shellEjectionPoint;
	private GameObject InstantiatedShell;
	

    void Update () {
    
    		string animString = animNumber.ToString ();
    		player = GameObject.FindGameObjectWithTag ("Player" + animString);

    		//shooting
    		if (Input.GetButton ("Fire_P" + animString) && Time.time > nextFire) {
    			nextFire = Time.time + fireRate;
    			InstantiatedObj = (GameObject)Instantiate (shot, shotSpawn.position, shotSpawn.rotation);
    			Destroy (InstantiatedObj, 3f);

    			//Muzzle Flash
    			Transform clone = Instantiate (MuzzleFlashPrefab, shotSpawn.position, shotSpawn.rotation) as Transform;
    			clone.parent = shotSpawn;
    			float position = Random.Range (0.0f, 0.0f);
    			float size = Random.Range (0.9f, 1.2f);
    			clone.localScale = new Vector3 (size, size, size);
    			clone.localPosition = new Vector3 (position, 0.4f, position);
    			Destroy (clone.gameObject, 0.1f);

    			//Shells    			
    			InstantiatedShell = (GameObject)Instantiate (shell, shellEjectionPoint.position, Quaternion.identity);
    			InstantiatedShell.GetComponent<Rigidbody2D> ().AddForce (-shellEjectionPoint.forward * Random.Range (180f, 220f));
    
    			Invoke ("laydown", 1f);
    			Destroy (InstantiatedShell, 3.0f);
    
    
    		}
}

“laydown” puts the Rigidbody2D to sleep:

void laydown(){
		InstantiatedShell.GetComponent<Rigidbody2D> ().Sleep ();

	}

What I got is this: Everytime the Fire button is pushed ONE shell flies out of the gun (in fact out of the screen…), every other shell after the first one is just created with no gravity and stays there for a certain time, before it’s destroyed. What I want is that every bullet flies out of the gun in an arc, stays there for a couple of seconds and disappears.

Could anyone be so gentle and help me please?
Thanks in advance!

The problem is your InstantiatedShell variable. It’s global to the class, so every time you’re instantiating a new shell, that variable references that latest one. So when your laydown function is called 3 seconds later, its putting to sleep the last shell that was created.

You can fix the scoping problem by using a local variable instead of a class variable…

GameObject InstantiatedShell = (GameObject)Instantiate(shell, shellEjectionPoint.position, Quaternion.identity);
             InstantiatedShell.GetComponent<Rigidbody2D> ().AddForce (-shellEjectionPoint.forward * Random.Range (180f, 220f));

This is declaring the InstantiatedShell variable locally. The problem now becomes, how do you put that object to sleep after 1 second?

First, consider if you even need your laydown function? Doesn’t a physics object sleep on its own after awhile once it stops moving?

If you still do, your best bet is to probably add a script on the shell gameobject itself. Have it track the amount of time that has passed since it was created, and then go to sleep.

float timer;

void Update()
{
  if (timer < 0) return;  // nothing to do if we're sleeping

  // otherwise track the time
  timer += Time.deltaTime;

  // go to sleep if we've reached our sleep threshold
  if (timer >= 1.0f)
  {
     GetComponent<RigidBody2D>().Sleep();
     timer = -1;  // stop tracking time
  }
}

Turn that into a full Monobehaviour and attach it to your bullet object. You can put the Destroy code in there as well and have the object destroy itself after 3 seconds.

I suggest using a Particle System.

Make it emit a Mesh with the material you want on the shell, configure the physics the way you want, set it up on Burst style, point it in the direction you want, keep a reference to it and every time you fire the weapon do shellSystem.Emit(1) (docs).

Works great =)