I am a new programmer in my school and i am working on my first school game project where i have a character that shoots bullets ( an object) to the world. I have the shooting mechanism right but the one thing i actually want to fix for this system is that the objects the Instantiate clones is limited to 5. I dont know how to limit the objects directly(Basically i want 5 objects to be cloned and if the it tries to summon a 6th object, the 5th object gets replaced by the 6th object). I have tried multiple tutorials and answers here in Unity but so far, no luck.
Here is the code
using UnityEngine;
using System.Collections.Generic;
public class ShootingScript : MonoBehaviour {
public Rigidbody Projectilebullet;
public float ProjectileVelocity = 10f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
if (Input.GetButton("Fire2") && Input.GetButton("Fire1"))
{
Rigidbody One;
One = Instantiate(Projectilebullet, transform.position, transform.rotation) as Rigidbody;
One.velocity = transform.TransformDirection(Vector3.forward * ProjectileVelocity);
}
}
}