Question by
bellwether · Jun 20, 2017 at 12:48 PM ·
instantiatespawnif statement
instantiate capacity
I need help getting my code to only allow 4 spawned objects in a scene at a time. I got the code to spawn up to 4 objects which is what I want but since I set it up to delete the clone object after sometime. I still need it to fave the ability to spawn more if the object count is less then four.
public GameObject shield;
GameObject shieldPrefabClone; //player transform private Transform chara; //enemy to lookfor. private Transform weapon; public float defendDistance;
int spawnNum = 1 ;
int maxSpawn = 4;
public int deleteTime ;
public bool Active = false;
List<GameObject> _shield;
void Start ()
{
_shield = new List<GameObject> ();
weapon = GameObject.FindWithTag("Eweapon").GetComponent<Transform>();
chara = GameObject.FindWithTag("Player").GetComponent<Transform>();
}
void Update ()
{
if ((Vector3.Distance (chara.transform.position, weapon.position) < defendDistance) && (_shield.Count < maxSpawn))
{
if (!Active)
{
Active = true;
spawner ();
}
}
else if ((Vector3.Distance (chara.transform.position, weapon.position) > defendDistance) || (_shield.Count >= maxSpawn))
{
if (Active)
{
Active = false;
Destroy (GameObject.Find ("Sphere(Clone)"), deleteTime);
print (deleteTime);
}
}
}
void spawner ()
{
for (int i = 0; i < spawnNum; i++) {
_shield.Add ((GameObject) Instantiate (shield, chara.transform.position + (Random.insideUnitSphere * 2) , Quaternion.identity) as GameObject);
}
}
}
Comment