- Home /
Spawning Enemy spawns more then MaximumEnemy?
o, SO i set up this simple enemy spawner and when you kill one it spawns another awesome, however I end up with 20 Medium enemies rather then a maximum of 5..... any Idea's on why its going over the limit?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnEnemy : Photon.MonoBehaviour {
public float maximumEnemy = 20;
public float maximumEnemyMedium = 5;
private GameObject SpawnLocation;
public GameObject[] Enemies;
public int index;
public GameObject[] Enemies2;
public int index2;
// Use this for initialization
void Start () {
SpawnLocation = this.gameObject;
}
// Update is called once per frame
void Update () {
if (PhotonNetwork.isMasterClient == true) {
Enemies = GameObject.FindGameObjectsWithTag ("EnemySmall");
index = Enemies.Length;
Enemies2 = GameObject.FindGameObjectsWithTag ("EnemyMedium");
index2 = Enemies2.Length;
if (index > maximumEnemy)
return;
GameObject Clone = PhotonNetwork.Instantiate ("Enemy", SpawnLocation.transform.position, SpawnLocation.transform.rotation, 0);
if (index2 > maximumEnemyMedium)
return;
GameObject Clone2 = PhotonNetwork.Instantiate ("EnemyMedium", SpawnLocation.transform.position, SpawnLocation.transform.rotation, 0);
}
}
//-----------------------------------------------------------------------------------------------------------
}
Hmm, sure you got the prefabs right as SpringWtaer suggests? Also sure those prefabs have the right tags. Once all that checks out change your approach. You Don't want to be looking for Gameobjects every frame that is wildly inefficient. Store the active list of enemies so you don't have to keep finding them.
As the other has been suggesting, double check you have the correct enemy assigned. Ensure that the Tags are correct for all.
Answer by springwater · Jul 09, 2017 at 06:41 AM
if (index > maximumEnemy) return; else// <------ would add "else" here I think and all such parts of the script. GameObject Clone = PhotonNetwork.Instantiate ("Enemy", SpawnLocation.transform.position, SpawnLocation.transform.rotation, 0);
not sure if that will help keep it clean.. but you might also check to make sure your medium enemy prefabs are actually the one you wanted to drag into the correct slots in the inspector..you could try steppng through the code with monos attachment feature.
Your answer
Follow this Question
Related Questions
radom spawn points 1 Answer
Need some help with Enemy Spawning Script... 1 Answer
Enemy Wave System Error -1 Answers
How to Spawn an enemy behind player? 1 Answer