So, my game was running REEEALLLY slow and i couldn’t figure out why, when i was only spawing 4 enemys. Then i figured out i was spawning 16 enemys. So what is wrong with this code, that makes it spawn 4 instead of one? I’ve looked through it, but i’m just learning how to use messages so can anyone help?
SpawnMessage
using UnityEngine;
using System.Collections;
public class SpawnMessage : MonoBehaviour {
// Use this for initialization
public IEnumerator Start() {
print(Time.time);
yield return new WaitForSeconds(5);
Messenger.Broadcast("spawn type1");
}
// Update is called once per frame
void Update () {
}
}
CreateMob
using UnityEngine;
using System.Collections;
public class CreateMob : MonoBehaviour {
public Transform thePrefabToInstantiate;
void OnEnable()
{
Messenger.AddListener("spawn type1",Spawn1);
}
void OnDisable() // to avoid reference problem on LoadLevel
{
Messenger.RemoveListener("spawn type1",Spawn1);
}
void Spawn1()
{
Instantiate(thePrefabToInstantiate, transform.position,transform.rotation);
}
}